简体   繁体   English

如何添加登录/注销链接(Django)

[英]How to add Login/Logout link (Django)

I need a link in django template which turns into logout if user is authenticated.我需要 django 模板中的一个链接,如果用户通过身份验证,该链接将变为注销。 (i have already implemented login/logout pages) (我已经实现了登录/注销页面)

tried {% if user.is_authenticated %} {% endif %} and {% if user.is_anonymous %} {% endif %} but didn't work.尝试{% if user.is_authenticated %} {% endif %}{% if user.is_anonymous %} {% endif %}但没有用。

Test Code (https://docs.djangoproject.com/en/dev/topics/auth/) -测试代码 (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 %}

Returns false evan after logged in successfully.登录成功后返回false evan。

It doesn't look like there is anything wrong with the template code you posted.您发布的模板代码看起来没有任何问题。 So I'd check out the associated view.所以我会检查相关的视图。 In particular, if you're using a custom-made view (rather than, say, a generic view), remember to supply a RequestContext to your template.特别是,如果您使用的是定制视图(而不是通用视图),请记住为您的模板提供RequestContext

From the Django tutorial, part 4 :来自Django 教程,第 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM