【问题标题】:Django--Is there a way to reference/get the current page URL from within a template? [duplicate]Django——有没有办法从模板中引用/获取当前页面 URL? [复制]
【发布时间】:2017-04-26 23:41:00
【问题描述】:

我的想法类似于

{% if <"the current page url = '/'"> %}
    {% include '...' %}  

如何使用模板标签完成这项工作?

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    您可以尝试request.path获取url路径。

    其他答案:

    您可以创建自定义模板上下文处理器,您可以在其中存储当前页面 url。

    链接:https://docs.djangoproject.com/en/1.10/ref/templates/api/#writing-your-own-context-processors

    【讨论】:

    • 创建自定义模板上下文处理器只是另一种说法“在您的视图中为 request.path 创建变量并将其传递给上下文”吗?或者它本质上是不同的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2015-11-07
    • 1970-01-01
    • 2011-10-24
    • 2014-05-26
    相关资源
    最近更新 更多