Django注销问题

时间:2018-08-13 15:19:08

标签: django python-3.x

我正在关注一个教程,但是注销无法正常工作,我有类似的内容。 (Python 3.7)

from django.contrib.auth import views as auth_views

urlpatterns = [
# Auth
url(r'^logout/', auth_views.logout, name='logout'),
]

我收到此错误(无法运行):

AttributeError: module 'django.contrib.auth.views' has no attribute 'logout'

我还检查了同一路径上是否有一个名为auth_logout()的函数,但是它需要一个request才能工作,我不知道该怎么做。它不带参数运行,当我调用url时,它确实注销了,但出现异常:

The view django.contrib.auth.logout didn't return an HttpResponse object. It returned None instead.

欢迎任何帮助。谢谢。

2 个答案:

答案 0 :(得分:2)

尝试:

    from django.contrib.auth import logout

    url(r'^logout/', logout, name='logout'),

或者您可以尝试:

    from django.contrib.auth import views as auth_views

    url(r'^logout/', auth_views.LogoutView.as_view(), name='logout'),

答案 1 :(得分:1)

Views模块不具有注销功能。下面的代码应该可以工作。

urlpatterns = [
    url(r'^logout/', auth_views.LogoutView.as_view(), name='logout'),
]