Last Updated: February 25, 2016
·
1.102K
· codybonney

Insert a websites absolute URI into a Django template

To add the absolute URI from a template, you can use Django's built in buildabsoluteuri method.

First, you need to add the request context preprocessor to your settings.py file.

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
)

Then, from a Django template, you can add the following tag to generate the full URI.

{{ request.build_absolute_uri }}

If you are using this to generate a url for a social media link, you can also pipe in the urlencode function to encode the uri you are generating.

{{ request.build_absolute_uri|urlencode }}

Source: Inserting the absolute URI into a Django template