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 }}
Written by Cody Bonney
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#