Python 删除Django tamplate中指向当前页面的链接
我有一个Django模板(list.html),其中包括以下链接Python 删除Django tamplate中指向当前页面的链接,python,django,django-templates,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 h
<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>
|
|
其中两个视图(通知:列表&通知:列表\未读)使用此模板,但发送不同的查询集以显示
如何使用Django模板语言删除指向当前视图的链接
例如,如果我在“列表”视图中,我会看到:
未读|标记所有已读
如果我在“列表未读”视图中,我会看到:
全部|标记全部已读
还是有更好的方法?这似乎是一项常见任务。您可以将一个变量传递到
上下文中
,并在模板中选中此项以禁用任一链接
通知列表视图:
在通知列表视图中,可以将变量通知\u列表
传递给模板
class NotificationsListView(..):
def get_context_data(self):
context = super(NotificationListView).get_context_data()
context['notifications_list'] = True
return context
然后在模板中,您可以执行以下操作:
{% if not notifications_list %}
<a href="{% url 'notifications:list' %}" role="button">All</a> |
{% else %}
<a href="{% url 'notifications:list_unread' %}" role="button">Unread</a> |
{% endif %}
<a href="{% url 'notifications:read_all' %}" role="button">Mark all read</a>
{%if notifications\u list%}
|
{%else%}
|
{%endif%}
因此,每当有通知列表视图请求时,列表
链接将被禁用,并显示列表_未读
链接
在
list\u unread
请求的情况下,将显示list
链接,而list\u unread
链接将不显示。以下是一种在模板中正确执行此操作的方法,将前两个链接显示为条件链接:
{% 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>
{%url'通知:列表作为列表\u url%}
{%if request.path!=列表\ url%}
|
{%endif%}
{%url'通知:将\u未读'列为列表\u未读\u url%}
{%if request.path!=列表\未读\ url%}
|
{%endif%}
h/t:你的问题不是很清楚你想解决什么问题。禁用当前视图的链接是什么意思?或者你是说你在重复使用
列表
和列表_未读
的模板时遇到问题?我的意思是,如果它们在“未读”页面上,我不希望“未读”链接可以点击。