问题描述
当用户使用Django内置的logout方法注销时,我一直在尝试将用户重定向到main。 但是,它一直向我发送这样的错误消息。 哪部分错了?
错误信息 :
MultiValueDictKeyError at /blog/logout/ "'path'"
views.py
from django.contrib.auth import authenticate, login, logout
@login_required
def logout(request):
logout(request)
return HttpResponseRedirect('blog/login.html')
urls.py
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
编辑01我试图找到路径变量但找不到它。 另外,我更改了注销方法代码。
@login_required
def logout(request):
logout(request)
return HttpResponseRedirect('/blog/login/')
这是在views.py中登录
def login(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
items = Article.objects.filter(is_visible=True)
request.session['username']=username
return render(request, 'blog/home.html', {'items': items})
else:
return render(request, 'blog/login.html', {'error_message': 'This account has been locked'})
else:
return render(request, 'blog/login.html', {'error_message': 'invaild login'})
return render(request, 'blog/login.html')
我不知道这是否相关,但我在settings.py中添加了这一行
#added for login authentification
LOGIN_REDIRECT_URL = 'home'
1楼
问题出在这一行:
return HttpResponseRedirect('blog/login.html')
HttpResponseRedirect需要一个URL路径而不是模板的位置/路径,我想你需要的是:
from django.http import HttpResponseRedirect
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout as django_logout
@login_required
def logout(request):
django_logout(request)
return HttpResponseRedirect('/blog/login/')
并且还更改这些登录视图行:
username = request.POST['username']
password = request.POST['password']
至 :
username = request.POST.get('username')
password = request.POST.get('password')