无法在Django中注销用户

时间:2014-08-27 03:43:41

标签: python django

我的Django项目中有一个函数可以在单击链接时注销用户。问题是当我点击退出链接时,我得到一条404,上面写着"没有用户匹配给定的查询"。我似乎无法弄清楚这里发生了什么,我仔细检查了文档,看起来我做的一切都很正确。

到目前为止,我发现注销的唯一方法是进入Django Admin并点击顶部的注销链接。我不确定它为什么会这样,但无论如何,下面是我的代码。

登录功能:

def user_login(request):
    loginform = LoginForm()
    new_user = NewUser()
    # Initialize variables with empty string
    invalid = ''
    disabled = ''
    # Make sure that the form was POSTed
    if request.method == 'POST':
        loginform = LoginForm(request.POST)
        if loginform.is_valid():
            # Grab the submitted values
            username = request.POST['username']
            password = request.POST['password']
            # Attempt to grab a User object with submitted credentials
            user = authenticate(username=username, password=password)
            # If a User object was returned and the User is active,
            # log in the user and redirect the User to their profile page
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect(reverse('users:detail', args=(user.username,)))
                # If the User is not active, display an error message and send them to the
                # login page
                else:
                    disabled = 'User is not active'
                    context = {'loginform': loginform, 'invalid': invalid, 'disabled': disabled}
                    return render(request, 'users/login.html', context)
            # If no User object was returned, display an error message and send them to the
            # login page
            else:
                messages.add_message(request, messages.INFO, 'The username or password you entered is incorrect. Please try again.')
                context = {'loginform': loginform, 'disabled': disabled, 'new_user': new_user}
                return render(request, 'users/login.html', context)
        # If the form is not valid, display an error message and send them to the
        # login page
        else:
            messages.add_message(request, messages.INFO, "In order to login, you must complete the form.")
            context = {'loginform': loginform, 'disabled': disabled, 'new_user': new_user}
            return render(request, 'users/login.html', context)
    # If the form was not POSTed, refresh the LoginForm and send them to the
    # login page
    else:
        loginform = LoginForm()
        context = {'loginform': loginform, 'invalid': invalid, 'disabled': disabled, 'new_user': new_user}
        return render(request, 'users/login.html', context)

退出功能:

def user_logout(request):
    logout(request)
    return HttpResponseRedirect(reverse('users:index'))

urls.py:

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^signup/$', views.new_user, name='new_user'),
    url(r'^login/$', views.user_login, name='u_login'),
    url(r'^browse/$', views.browse, name='browse'),
    url(r'^(?P<username>\w+)/$', views.detail, name='detail'),
    url(r'^(?P<username>\w+)/account/$', views.account, name='account'),
    url(r'^logout/$', views.user_logout, name='u_logout'),
)

点击链接以便退出:

<li><p><a href="{% url 'users:u_logout' %}" class="t-w-t">Logout</a></p></li>

我确保使用Django身份验证系统所需的设置已经到位。在此先感谢您的帮助,如果我需要发布更多代码,请告诉我。

**编辑**

我已经想出了一种方法来完成这项工作,虽然我觉得它很脏并且不是最好的方法。我已将urls.py更改为:

url(r'^(?P<username>\w+)/logout/$', views.user_logout, name='u_logout'),

并且视图功能为:

def user_logout(request, username):
    logout(request)
    return HttpResponseRedirect(reverse('users:index'))

现在,在模板中我还将用户名传递给视图,它可以正常工作。再说一遍,我觉得这个因为某种原因很脏,但它确实有效。如果有人有更好的解决方案,那就太棒了。

1 个答案:

答案 0 :(得分:2)

问题与您的注销功能本身无关。它只是您的URL配置的一个功能。您&#34;详细信息的模式&#34;函数只是r'^(?P<username>\w+)/$'匹配字符串&#34; logout&#34;。因此,当用户点击注销链接时,Django会将其路由到&#34;详细信息&#34;视图,它将尝试查找名为&#34; logout&#34;的用户并且看错了。

您可以通过移动&#34;退出&#34;轻松解决此问题。 &#34;细节&#34;以上的模式在urls.py中有一个,因为URL按顺序匹配。