2017-03-21 3 views
0

私はDjangoの初心者です。ユーザーがスタッフである場合にのみリンクを表示する必要があるナビゲーションバーがあります。以下は私のナビゲーションバーですユーザーがDjangoのスタッフメンバーである場合のテンプレートへのリンクを表示

<div id="navbar" class="navbar-collapse collapse" aria-expanded="false" style="height: 1px;"> 
    <ul class="nav navbar-nav navbar-right"> 
     <li><a href="{% url 'pledges:preferences' %}">{% trans 'Preferences' %}</a></li> 
     <li><a href="{% url 'pledges:account' %}">{% trans 'My Account' %}</a></li> 
     {# The following link should be displayed just to staff members #} 
     <li><a href="{% url dashboard %}">{% trans 'Dashboard' %}</a></li> 
     <li><a href="{% url 'pledges:logout' %}">{% trans 'Log Out' %}</a></li> 
    </ul> 
</div> 

どのように私はこれを解決することができますか?それは同じくらい簡単です

{% if request.user.is_staff %} 
    <li><a href="{% url dashboard %}">{% trans 'Dashboard' %}</a></li> 
{% endif %} 

答えて

2

ビューに依存して、のような単純なものかもしれませんあなたはDjangoのバニラユーザーを使用する場合によっては

userは、関連するユーザモデルである
{# The following link should be displayed just to staff members #} 
{% if request.user.is_staff %} 
    <li><a href="{% url dashboard %}">{% trans 'Dashboard' %}</a></li> 
{% endif %} 

、またはあなたはそれを上書きしています。

+0

ありがとうございます。これは魅力的に働いています。それはまさに私が望んでいたものであり、私は文脈の中で何かを渡すことを心配していました。 – lmiguelvargasf

+2

歓迎します:-)関連するドキュメントは次のとおりです。https://docs.djangoproject.com/en/1.10/topics/auth/default/#authentication-data-in-templates – arie

3

関連する問題