2012-04-04 25 views
1

我需要django模板中的鏈接,如果用戶已通過身份驗證,它將變成註銷。 (我已經實現登錄/註銷頁面)如何添加登錄/註銷鏈接(Django)

嘗試{% if user.is_authenticated %} {% endif %}{% if user.is_anonymous %} {% endif %}但沒有奏效。

測試代碼(https://docs.djangoproject.com/en/dev/topics/auth/) -

{% if user.is_authenticated %} 
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p> 
{% else %} 
    <p>Welcome, new user. Please log in.</p> 
{% endif %} 

返回埃文登錄成功後。

+0

使用'{%其他%}'爲好。 – Blender 2012-04-04 02:51:43

+0

@Blender是的,嘗試與此同時使用。 – ChamingaD 2012-04-04 02:54:09

+0

然後你編碼錯了。發佈您的模板代碼。 – Blender 2012-04-04 02:54:33

回答

4

它看起來不像您發佈的模板代碼有任何問題。所以我會查看相關的視圖。特別是,如果您使用的是定製視圖(而不是通用視圖),請記住爲您的模板提供RequestContext

Django tutorial, part 4

from django.template import RequestContext 
# ... 
def detail(request, poll_id): 
    p = get_object_or_404(Poll, pk=poll_id) 
    return render_to_response('polls/detail.html', {'poll': p}, 
          context_instance=RequestContext(request)) 
+0

謝謝:)它通過添加'context_instance = RequestContext(請求)' – ChamingaD 2012-04-04 03:49:14

+1

您可以使用['render'快捷方式](http://django.me/render) – 2012-04-04 04:23:50