【问题标题】:django check within template if the current url has a particular word in itdjango 在模板中检查当前 url 中是否包含特定单词
【发布时间】:2012-10-04 08:33:32
【问题描述】:

类似于这个问题:

Django template, if tag based on current URL value

我只想检查网址中是否有单词 - 你知道,比如

if a.find(b) == -1

我该怎么做?

【问题讨论】:

  • downvoters - 关于这个问题的评论,也许?

标签: django


【解决方案1】:

Perhaps the in operator?

{% if b in request.path %}
    hello world!
{% endif %}

如果这不能满足您的需要,则将逻辑移至您的视图或创建custom template tag.

【讨论】:

  • 澄清:为此,您必须确保将请求对象传递给模板
  • 注意这不会搜索url中的GET参数
【解决方案2】:

忘记request参数并使用:

{% if 'search_term' in request.GET %}
  Hello World!
{% endif %}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-10
    • 2016-08-19
    • 2011-10-28
    • 2013-12-23
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    相关资源
    最近更新 更多