Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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().