Py学习  »  问与答

django如何在用户登录后返回到原来来页面,并保持登陆状态

1277859938 • 8 年前 • 3653 次点击  

各位朋友们好,现在用django做一个用户登录页面,希望能够在用户登录后返回到原来来页面,并保持登陆状态,网上找到了下面这个方法 def login_user(request): if request.method == 'GET': #记住来源的url,如果没有则设置为首页('/') request.session['login_from'] = request.META.get('HTTP_REFERER', '/') #TODO:显示登陆页面,blablabla if request.method == 'POST': #TODO: 用户登录操作,blablabla #重定向到来源的url return HttpResponseRedirect(request.session['login_from']) 这个方法有一个问题就是,虽然通过重定向返回到原来登陆的页面,但是仍然显示未登陆的状态,我本来想传一个request.user 作为参数传递给展示页面的,但是HttpResponseRedirect不能传递参数,希望朋友们能够指点一下,万分感谢了。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1272
 
3653 次点击  
文章 [ 1 ]  |  最新文章 8 年前
MCC
Reply   •   1 楼
MCC    8 年前
def login(request):
    # blahblah
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth.login(request, user)
    # blahblah 我后面是返回jason然后让前端刷新页面,代码就不贴了。。