To use django's build_absolute_uri()
function without including the query parameters, you can use the scheme
, netloc
, path
, and fragment
attributes of the HttpRequest
object to build the URL and then append any query parameters separately. Here's an example:
from django.http import HttpRequest
def my_view(request):
# Get the base URL
base_url = request.scheme + '://' + request.get_host() + request.path
# Append any fragment
url = base_url + '#section-1'
# Return the URL
return url
In this example, we first get the base URL by concatenating the scheme
, netloc
, and path
attributes of the HttpRequest
object. We then append any fragment
(e.g., #section-1
) to the URL separately. By not including any query parameters, we avoid using build_absolute_uri()
.
Asked: 2023-03-15 11:00:00 +0000
Seen: 11 times
Last updated: Sep 12 '22