How to get full url from django request

You can get full URL using request.build_absolute_uri method:

FULL_URL_WITH_QUERY_STRING: request.build_absolute_uri()
FULL_URL: request.build_absolute_uri('?')
ABSOLUTE_ROOT: request.build_absolute_uri("https://stackoverflow.com/")[:-1].strip("https://stackoverflow.com/")
ABSOLUTE_ROOT_URL: request.build_absolute_uri("https://stackoverflow.com/").strip("https://stackoverflow.com/")

Should this will help full to you.

The best way to use ABSOLUTE URLS in Django, you can create a context_processors or middleware and find your ABSOLUTE_URL and return that so that you can use any where in Django.

Like this example:

def absolute(request):
    urls = {
        'ABSOLUTE_ROOT': request.build_absolute_uri("https://stackoverflow.com/")[:-1].strip("https://stackoverflow.com/"),
        'ABSOLUTE_ROOT_URL': request.build_absolute_uri("https://stackoverflow.com/").strip("https://stackoverflow.com/"),
    }

    return urls

And Then you should use {{ABSOLUTE_ROOT}} in any where into you django template.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)