Luc*_*rgi 0 python django django-forms django-authentication django-login
在我的代码中,我目前正在使用@login_required装饰器.据我所知(并且可以在URL中看到),这会将我的登录视图传递给保存用户所在的原始页面的下一个参数.在检查用户的凭据后,我正在尝试将其重定向到原始页面,但我无法弄清楚如何执行此操作.我敢肯定这一定非常简单,但我已经坚持了很长一段时间而且在扔掉键盘之前我不得不寻求帮助.我的观看代码如下:
from django.contrib.auth import login as auth_login, authenticate
def login(request):
form_class = LoginForm
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
auth_login(request, user)
return redirect(?????????)
else:
#Error message
return render(request, 'login.html', {'form':form_class})
Run Code Online (Sandbox Code Playgroud)
检查登录成功后,您只需执行以下操作:
next_url = request.GET.get('next')
if next_url:
return HttpResponseRedirect(next_url)
else:
return render(request, 'main/main.html')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3573 次 |
| 最近记录: |