Ask Your Question
3

What is the way to use django's build_absolute_uri function without including the query parameters?

asked 2023-03-15 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-12 20:00:00 +0000

ladyg gravatar image

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

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-03-15 11:00:00 +0000

Seen: 8 times

Last updated: Sep 12 '22