简体   繁体   English

Django 登录页面下一页不工作

[英]Django next page not working in login page

I open my browser, and try to access my Django site.我打开浏览器,尝试访问我的 Django 网站。 I'm logged out of my account, so I get redirected to my login page, with the page I'm going to as a GET parameter.我退出了我的帐户,所以我被重定向到我的登录页面,我将要访问的页面作为 GET 参数。

http://website.com/login/?next=/customer/7/54

However, when I enter my login information, I get redirected to:但是,当我输入登录信息时,我被重定向到:

http://website.com/

Even though, I'm trying to get to:尽管如此,我正在努力做到:

http://website.com/customer/7/54

/customer/7/54 points to a view that does have the @login_required decorator, but I was under the assumption, that it'd still point to that view after I logged in.What is going on? /customer/7/54 指向一个确实有 @login_required 装饰器的视图,但我假设在我登录后它仍然指向那个视图。这是怎么回事?

EDIT: So I realized, I had a hidden input tag:编辑:所以我意识到,我有一个隐藏的输入标签:

    <input type="hidden" name="next" value='/'>

However, how do I pass in the GET paramter '/customer/7/54/ into the login template?但是,如何将 GET 参数 '/customer/7/54/ 传递到登录模板中?

According to your edited question, you can get next parameter at login page as follows: 根据您编辑的问题,您可以在登录页面获取下一个参数,如下所示:

<input type="hidden" name="next" value="{{ next }}">

The standart django's django.contrib.auth.views.login passes next variable directly to the template context. 标准django的django.contrib.auth.views.loginnext变量直接传递给模板上下文。 Also, read docs about user authentication : 另外,阅读有关用户身份验证的文档:

You can also specify the name of the GET field which contains the URL to redirect to after login by passing redirect_field_name to the view. 您还可以通过将redirect_field_name传递给视图来指定GET字段的名称,该字段包含要在登录后重定向到的URL。 By default, the field is called next. 默认情况下,该字段将被调用。

But you, probably, don't need to change this. 但是,你可能不需要改变它。

For people using their own view. 对于使用自己观点的人。 You have to send "next" to your template 您必须向模板发送“下一步”

def login_view(request):
    '''..your logic..'''
    ctx = {'form':form,'next':next}
    return render_to_response('login.html',ctx,context_instance=RequestContext(request))

You can write your custom view function for login like this:您可以像这样为登录编写自定义视图 function:

in views.pyviews.py

def login(request):
    next = request.GET.get('next')
    if request.method == 'POST':
        ...
        # if user logged in
        if request.GET.get('next') != None:
          return redirect(request.GET.get('next')) 
        return redirect('/')
    else: # request.method == get 
        return render(request, 'login.html', {'next': next})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM