【问题标题】:Django Template--Conditional Based on The Current URL PathDjango 模板--基于当前 URL 路径的条件
【发布时间】:2018-08-21 01:41:18
【问题描述】:

我正在尝试从我网站的“博客”部分中排除某些内容,并希望在以 /blog 开头的任何路径上排除此信息,其中包括主 /blog 页面和任何其他相关页面包括blog/<blog-post> 等。我查看了this post 并尝试了这里提到的一些建议,但不能完全让它发挥作用。这是我的两个博客网址:

url(r'^$', BlogListView.as_view(), name='blog'),
url(r'^(?P<slug>[\w-]+)/$', blog_post, name='blog_post')

以及我在 django 模板中尝试过的(未成功):

{% url 'blog:blog_post' slug=slug as the_url %}
{% if request.path == the_url %}
   <div>&nbsp;</div>

{% else %}

<div class="container">
    <div class="nav-2">
      <ul class="nav nav-pills">

          {% block side_block %}

          {% get_category_list %}

            {% endblock %}

      </ul>

    </div>
    </div>
{% endif %}

【问题讨论】:

    标签: python django


    【解决方案1】:

    好的,感谢this post,我知道了

    {% if '/blog/' in request.path %}DO SOMETHING HERE{% endif %}
    

    【讨论】:

      猜你喜欢
      • 2011-11-19
      • 2017-08-31
      • 2021-04-12
      • 2012-04-16
      • 2014-05-26
      • 1970-01-01
      • 2015-06-02
      • 2012-04-13
      • 1970-01-01
      相关资源
      最近更新 更多