logout()得到了意外的关键字参数“ next_page”

时间:2019-04-22 18:42:22

标签: python-3.7 django-1.11 django-2.2

Django 2.2中是否没有用于注销和登录的next_page或template_name参数?从Django 1.11升级到Django 2.2时出现这些错误!

这是我的urls.py

from django.contrib.auth import logout

url(r'^logout/$',logout, {'next_page': '/'},name='logout'),

settings.py的logout_url是

LOGOUT_URL = '/'

我不断收到此错误:

TypeError at /portal/logout/
logout() got an unexpected keyword argument 'next_page'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.2
Exception Type: TypeError
Exception Value:    
logout() got an unexpected keyword argument 'next_page'

登录也发生了同样的事情

urls.py

from django.conf.urls import url
from landing.views import landing_validation

app_name='landing'
urlpatterns = [
    url(r'^$', landing_validation, name='landing')
]

views.py

def landing_validation(request):
  login_response = login(request, template_name='landing.html')

  return login_response

TypeError at /
login() got an unexpected keyword argument 'template_name'
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 2.2
Exception Type: TypeError
Exception Value:    
login() got an unexpected keyword argument 'template_name'

1 个答案:

答案 0 :(得分:3)

如果您仍然想在迁移后解决该问题,那么这里是最简单的解决方法:

settings.py中添加:

LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'home'

home是指您的首页路由name或仅

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/'  # Or maybe another URL you want to set.

,然后在您的urls.py中将路线更改为这样:

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

LogoutView是导入的from django.contrib.auth.views import LogoutView