禁用Django中当前页面的链接

时间:2015-08-24 18:33:41

标签: python django django-templates

我有一个Django模板(list.html),其中包含这些链接

  <a href="{% url 'notifications:list' %}" role="button">All</a> |
  <a href="{% url 'notifications:list_unread' %}" role="button">Unread</a> |
  <a href="{% url 'notifications:read_all' %}" role="button">Mark all read</a>

其中两个视图(通知:列表和通知:list_unread)使用此模板,但发送不同的查询集以进行显示。

如何使用Django模板语言禁用当前视图的链接?

或者有更好的方法吗?这似乎是一项普遍的任务。

1 个答案:

答案 0 :(得分:0)

这是在模板中正确执行此操作的方法,将前两个链接显示为条件:

  {% url 'notifications:list' as list_url %}
  {% if request.path != list_url %}
    <a href="{{list_url}}">All</a> |
  {% endif %}
  {% url 'notifications:list_unread' as list_unread_url %}
  {% if request.path != list_unread_url %}
    <a href="list_unread_url" >Unread</a> |
  {% endif %}
  <a href="{% url 'notifications:read_all' %}">Mark all read</a>

h/t:https://stackoverflow.com/a/17311506/2700631

相关问题