Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何添加登录/注销链接(Django)_Python_Django - Fatal编程技术网

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

Python 如何添加登录/注销链接(Django),python,django,Python,Django,我需要一个链接在django模板变成注销,如果用户是认证。(我已经实现了登录/注销页面) 尝试了{%if user.is_authenticated%}{%endif%}和{%if user.is_anonymous%}{%endif%},但无效 测试代码(https://docs.djangoproject.com/en/dev/topics/auth/)- {%if user.u经过身份验证%} 欢迎,{{user.username}}。感谢您登录 {%else%} 欢迎新用户。请登录 {%

我需要一个链接在django模板变成注销,如果用户是认证。(我已经实现了登录/注销页面)

尝试了
{%if user.is_authenticated%}{%endif%}
{%if user.is_anonymous%}{%endif%}
,但无效

测试代码(https://docs.djangoproject.com/en/dev/topics/auth/)-

{%if user.u经过身份验证%}
欢迎,{{user.username}}。感谢您登录

{%else%} 欢迎新用户。请登录

{%endif%}

成功登录后返回falseevan。

您发布的模板代码似乎没有任何问题。所以我要查看相关的视图。特别是,如果您使用的是自定义视图(而不是一般视图),请记住为模板提供一个

从:


您发布的模板代码看起来没有任何问题。所以我要查看相关的视图。特别是,如果您使用的是自定义视图(而不是一般视图),请记住为模板提供一个

从:


@Blender是的,也尝试过使用它。然后你编码错误了。发布你的模板代码。应该可以。是
用户。您的身份验证设置是否正确,还是始终设置为
True
?@Blender这是我从中获取的测试代码(不用于登录/注销)@Blender您所说的正确设置是什么意思<代码>{%if user.is_authenticated%}
总是返回false。@Blender是的,也尝试过使用它。那么您的代码编写错误了。发布你的模板代码。应该可以。是
用户。您的身份验证设置是否正确,还是始终设置为
True
?@Blender这是我从中获取的测试代码(不用于登录/注销)@Blender您所说的正确设置是什么意思<代码>{%if user.is\u authenticated%}
始终返回false。谢谢:)它通过添加
context\u实例=RequestContext(请求)
谢谢:)工作,它通过添加
context\u实例=RequestContext(请求)
{% if user.is_authenticated %}
    <p>Welcome, {{ user.username }}. Thanks for logging in.</p>
{% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}
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))