Emanuel
Emanuel

Reputation: 165

request.path in django template

I try 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 does not that work?

Upvotes: 9

Views: 42510

Answers (4)

okumu justine
okumu justine

Reputation: 368

Try this:

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

Upvotes: 8

Daniele Leonardi
Daniele Leonardi

Reputation: 1

before 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))

Upvotes: 0

freakish
freakish

Reputation: 56477

By default Django's template processors are

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 to use request in templates, so add it to that list in settings.py. If you don't have that variable there then set it.

Upvotes: 17

Brandon Taylor
Brandon Taylor

Reputation: 34553

Try:

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

Upvotes: 4

Related Questions