Request.path in django template

I am trying something like this:

{% if request.path == 'contact' %} <p>You are in Contact</p> {% endif %} {% if request.path == 'shop' %} <p>You are in Shop</p> {% endif %} 

Why is this not working?

+4
source share
4 answers

Default Django Template Processors

 TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.core.context_processors.static", "django.core.context_processors.tz", "django.contrib.messages.context_processors.messages" ) 

(see documentation )

You need django.core.context_processors.request use request in the templates, so add it to this list in settings.py . If you do not have this variable, set it.

+15
source

Try:

 {% if request.path == '/contact/' %} <p>You are in Contact</p> {% elif request.path == '/shop/' %} <p>You are in Shop</p> {% endif %} 
+2
source

up to 1.8 settings.py

 TEMPLATE_CONTEXT_PROCESSORS = ( 'other.required.processors.names', 'django.core.context_processors.request', ) 

views.py (using className.as_view)

 from django.template import * class className(TemplateView): template_name = "name.html" 

views.py (normal use)

 from django.shortcuts import render_to_response def name(request): return render_to_response('name.html'{},context_instance=RequestContext(request)) 
0
source

Try it:

 {% if 'contact' in request.path %} 
0
source

Source: https://habr.com/ru/post/1469553/


All Articles