Make sure you have django.template.context_processors.request
listed in your context_processors
.
As of Django 1.9 version, it already comes configurated. The default TEMPLATES
configuration looks like that:
To get the current path:
Current path with querystring:
Domain, path and querystring:
Outputs
Considering we are acessing the following URL: http://127.0.0.1:8000/home/?q=test
Method | Output |
---|---|
request.path |
/home/ |
request.get_full_path |
/home/?q=test |
request.build_absolute_uri |
http://127.0.0.1:8000/home/?q=test |
Troubleshooting
Django 1.7 or below
If you are using an older version of Django (<= 1.7) where the TEMPLATES
configuration is not available, you can include the
context processor like this:
settings.py
Notice the context processor was available inside the core
module. Since version >= 1.8
it is available inside the
template
module.