为什么我无法注销django用户的身份验证? [英] Why can't I logout on django user auth?

查看:78
本文介绍了为什么我无法注销django用户的身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django.contrib.auth用户管理系统.

I am using the django.contrib.auth user management system.

所以我注册/插入到用户表/模型中,然后从django.contrib.auth.views.login登录,这样我就可以登录.

So I got the registration/insert into the user table/model up and the login from django.contrib.auth.views.login up so I can log in.

但是,我无法使用django.contrib.auth.views.logout退出

However, I can't use django.contrib.auth.views.logout to logout

我的模板中有

<h1>My Account</h1>
<strong> Welcome, {{ name|capfirst }}!</strong>
<br /><br />
<ul>
    <li>
        {% if user.is_authenticated %}
            <a href="{% url django.contrib.auth.views.logout %}">Logout</a>
        {% else %}
            <a href="{% url register %}">Sign Up</a>
    </li>
    <li>
            <a href="{% url django.contrib.auth.views.login %}">Login</a>
        {% endif %}
    </li>
</ul>

但是我总是得到名称和注销链接,因为当我单击注销按钮时我从未真正注销过

However I always get the name and the logout link because I never actually logout when I click on the logout button

这是我的urls.py部分:

Here is my urls.py section for this:

urlpatterns += patterns('django.contrib.auth.views',
    url(r'^login/$', 'login', { 'template_name': 'registration/login.html', 'SSL': settings.ENABLE_SSL }, 'login' ),
    url(r'^my_account/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),
)

我做错了什么? 注意:我也通过apache2和mod_wsgi运行django

What am I doing wrong? Note: I am also running django via apache2 with mod_wsgi

谢谢!

添加的信息:

不确定这是否有帮助,但是我在html中打印了request.session.items并得到了

Not sure if this helps but I printed request.session.items in the html and got

[('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 9L)] 

登录时以及单击注销按钮(django.contrib.auth.views.logout)

when I was logged in and also after I clicked the logout button (django.contrib.auth.views.logout)

我还创建了:

from django.contrib.auth import logout
def logout_view(request):
    request.session.items = []
    request.session.modified = True
    logout(request)

并将其链接到第二个注销链接/按钮,但我没有注销,并且单击链接后,request.session.items与上面相同

And linked that to a second logout link/button and I didn't logout and the request.session.items stayed the same as above after clicking the link

我想快点进去

在我的一种查看功能中,我做到了:

In one of my view functions I did:

request.session["fav_color"] = "blue"
request.session.modified = True

,然后以HTML {{request.session.items}}打印,这给了我

and then print in html {{ request.session.items }} which gave me

    [('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 9L)] 

,并且没有("fav_color","blue")元组.我还是再次做错了,还是证明我的request.session列表没有被修改?

and no ('fav_color', 'blue') tuple. Did I do something wrong again, or is this proof that my request.session list isn't being modified?

K知道了:

url(r'^my_account/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),

应该是

url(r'^logout/$', 'logout', { 'template_name': 'registration/my_account.html', 'SSL': settings.ENABLE_SSL }, 'logout' ),

推荐答案

我认为urls.py可能像这样(

I think that urls.py could be like this(login and logout views do not accept SSL parameter):

from django.core.urlresolvers import reverse
urlpatterns += patterns('django.contrib.auth.views',
        url(r'^login/$', 'login', { 'template_name': 'registration/login.html'}, name='login' ),
        url(r'^logout/$', 'logout', { 'template_name': 'registration/my_account.html', 'next_page':reverse('index') }, name='logout' ),
)

在模板中:

<h1>My Account</h1>
<strong> Welcome, {{ name|capfirst }}!</strong>
<br /><br />
<ul>
    <li>
        {% if user.is_authenticated %}
            <a href="{% url logout %}">Logout</a>
        {% else %}
            <a href="{% url register %}">Sign Up</a>
    </li>
    <li>
            <a href="{% url login %}">Login</a>
        {% endif %}
    </li>
</ul>

这篇关于为什么我无法注销django用户的身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆