gpt4 book ai didi

python - 在 Django 中注销用户时遇到问题

转载 作者:行者123 更新时间:2023-12-01 05:03:31 25 4
gpt4 key购买 nike

我的 Django 项目中有一个函数,可以在单击链接时注销用户。问题是,当我单击注销链接时,我收到 404 消息,其中显示消息“没有用户与给定的查询匹配”。我似乎无法弄清楚这里发生了什么,我仔细检查了文档,看起来我做的一切都是正确的。

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

登录功能:

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'))

url.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'),

和 View 函数:

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

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

最佳答案

该问题与您的注销功能本身无关。它只是 URL 配置的一个函数。您的“详细信息”功能的模式只是 r'^(?P<username>\w+)/$' ,它也将匹配字符串“logout”。因此,当用户单击注销链接时,Django 会将其路由到“详细信息” View ,该 View 将尝试查找名为“logout”的用户,但会失败并显示您看到的错误。

您可以通过将 urls.py 中的“注销”模式移至“详细信息”模式上方来轻松解决此问题,因为 URL 是按顺序匹配的。

关于python - 在 Django 中注销用户时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25518604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com