2015-08-24 48 views
0

我有Django模板(list.html),其中包括這些鏈接禁用鏈接到當前頁面在Django

<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

你的問題不是很清楚你試圖解決什麼問題。你是什​​麼意思'禁用「當前視圖的鏈接?還是你說你有麻煩重複使用一個模板的'list'和'list_unread'? –

+0

我的意思是,如果他們在「未讀」頁面上,我不希望「未讀」鏈接可點擊。 – 43Tesseracts

回答

0

您可以將一個變量傳遞到context並檢查這是否在您的模板中以禁用任一鏈接。

通知列表查看:

在您的通知列表視圖中,你可以通過一個變量notifications_list到您的模板。

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> 

所以,每當有對通知列表視圖的請求,該list鏈接將被禁用,list_unread鏈接將顯示。

如果是list_unread請求,將顯示list鏈接,並且list_unread鏈接將不會顯示。