Django Absolute provides context processors and template tags to use full absolute URLs in templates.
You can install Django Absolute with pip:
pip install django-absolute
or with easy_install:
easy_install django-absolute
Add absolute
to your settings.INSTALLED_APPS
.
Add absolute.context_processors.absolute
to your template context processors. Note that this requires the request
context processor shipped with Django.
Then you can access the following variables in your templates:
ABSOLUTE_ROOT
: full absolute root URL (without trailing slash) based on incoming requestABSOLUTE_ROOT_URL
: full absolute root URL (with trailing slash) based on incoming requestSITE_ROOT
: full absolute root URL (without trailing slash) based on current Django SiteSITE_ROOT_URL
: full absolute root URL (with trailing slash) based on current Django site
Django absolute provide 2 template tags:
absolute
: acts likeurl
but provide a full URL based on incoming request.site
: acts likeurl
but provide a full URL based on current Django Site.
To use theses template tags, you need to load the absolute
template tag library.
{% load absolute %}
{% url "my_view" %}
{% absolute "my_view" %}
{% site "my_view" %}
These template tags have exactly the same syntax as url
, including the "as" syntax:
{% absolute "my_view" as the_url %}
{{ the_url }}
You may optionally pass a custom site
to the {% site %}
templatetag to always make it used that given site:
{% site "my_view" site my_site_obj ...url args... %}
{% site "my_view" %}
guesses the from the current request if HTTP or HTTPS should be used. To force a protocol (for example if using without a request), use the ABSOLUTE_URL_PROTOCOL
setting:
ABSOLUTE_URL_PROTOCOL = 'https'