How to get the current url in the django template

created at 07-17-2021 views: 2

In actual development, it is often necessary to get the current URL in the template. The method of obtaining is as follows:

django version 1.9 and above

## template file
{{ request.path }} # Without request.get parameters
{{ request.get_full_path }} # With request.get parameters

Django versions before 1.9

## settings.py
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.core.context_processors.request',)

## views.py
from django.shortcuts import render_to_response
from django.template import RequestContext

def index(request):
    return render_to_response(
        'user/profile.html',
        { 'title': 'User profile' },
        context_instance=RequestContext(request)
    )


## template
{{ request.path }}
Please log in to leave a comment.