request.path in django template

前端 未结 4 600
栀梦
栀梦 2020-12-19 13:42

I try something like this:

{% if request.path == \'contact\' %}
    

You are in Contact

{% endif %} {% if request.path == \'shop\' %}
相关标签:
4条回答
  • 2020-12-19 13:49

    Try this:

    {% if 'contact' in request.path %}
    
    0 讨论(0)
  • 2020-12-19 14:02

    Try:

    {% if request.path == '/contact/' %}
        <p>You are in Contact</p>
    {% elif request.path == '/shop/' %}
        <p>You are in Shop</p>
    {% endif %}
    
    0 讨论(0)
  • 2020-12-19 14:02

    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))
    
    0 讨论(0)
  • 2020-12-19 14:08

    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.

    0 讨论(0)
提交回复
热议问题