Django模板中的request.path [英] request.path in django template

查看:149
本文介绍了Django模板中的request.path的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试这样的事情:

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

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

为什么这样不起作用?

推荐答案

默认情况下,Django的模板处理器为

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

(请参阅文档

您需要 django .core.context_processors.request 以在模板中使用 request ,因此将其添加到 settings.py 中的列表中。如果没有该变量,则进行设置。

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.

这篇关于Django模板中的request.path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆