登录后django重定向不工作“下一个”不张贴? [英] django redirect after login not working "next" not posting?

查看:102
本文介绍了登录后django重定向不工作“下一个”不张贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录页面正常工作,但重定向到引荐来源网页的除外。用户在应用程序中收到一个直接链接的电子邮件,他们(在这个例子中)没有登录,并被重定向到登录页面。登录成功后,用户被重定向到硬编码的路径。请参见下面的示例。

I have a login page which is working fine with the exception of the redirect to the referrer page. The user gets an email with a direct link within the app, they (in this example) are not logged in already and are redirected to the login page. After successful login the user is redirected to a hardcoded path. See example below.

电子邮件中的URL: http:// localhost:8000 / issueapp / 1628 / view / 22

URL in email: http://localhost:8000/issueapp/1628/view/22

登录页面的URL: http:// localhost:8000 / login?next = / issueapp / 1628 / view / 22

登录视图(使用硬编码重定向):

Login view (with hardcoded redirect):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.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)
                state = "You're successfully logged in!"
                return HttpResponseRedirect('/issueapp/1628/')
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

登录视图(带下一个重定向):

Login view (with "next" redirect):

def login_user(request):    
    state = "Please log in below..."
    username = password = ''

    if request.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)
                state = "You're successfully logged in!"
                return HttpResponseRedirect(request.GET['next'])
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username
        },
        context_instance=RequestContext(request)
    )

上面的视图导致一个例外Key'next'未在< QueryDict:{}>中找到该表单似乎不会发布下一个变量,即使它在那里在url和形式。
我搜索过,看到无处不在,不明白为什么它不工作。任何想法?

The above view results in an exception "Key 'next' not found in <QueryDict: {}>" The form does not appear to be posting the "next" variable, even though its there in the url and in the form. I have searched and looked everywhere and cant figure out why its not working. Any ideas?

其他参考:

登录模板:

{% block content %}

    {{ state }}
    <form action="/login/" method="post" >
                {% csrf_token %}
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        username:
        <input type="text" name="username" value="{{ username }}" /><br />
        password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In"/>

        {% debug %}
    </form>
{% endblock %}

以下是现在为我工作的代码(感谢保罗布的帮助)! **

The below is the code which is now working for me (thanks to the help of Paulo Bu)! **

登录查看:

def login_user(request):

    state = "Please log in below..."
    username = password = ''

    next = ""

    if request.GET:  
        next = request.GET['next']

    if request.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)
                state = "You're successfully logged in!"
                if next == "":
                    return HttpResponseRedirect('/issueapp/')
                else:
                    return HttpResponseRedirect(next)
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response(
        'account_login.html',
        {
        'state':state,
        'username': username,
        'next':next,
        },
        context_instance=RequestContext(request)
    )

登录模板:

{{ state }}

{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}

            {% csrf_token %}

    username:
    <input type="text" name="username" value="{{ username }}" /><br />
    password:
    <input type="password" name="password" value="" /><br />

    <input type="submit" value="Log In"/>

    {% debug %}
</form>


推荐答案

你的代码很好,唯一的问题是您将下一个属性的形式作为帖子,因为该方法是 post 。在这些视图中,您尝试在 get 字典中获取下一个参数,这显然不是那样。

Your code is fine, the only problem is that in the form you are passing the next attribute as a post because the method is post. In the views you try to get the next parameter within the get dictionary which is obvious not there.

您必须声明html表单操作,以便您的浏览工作。

You have to declare the html form action like this in order to your views work.

{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
        {% csrf_token %}
        username:
        <input type="text" name="username" value="{{ username }}" /><br />
        password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In"/>

        {% debug %}
    </form>

如果有一个下一个变量那么您将包含在 url 中,将其作为get参数进行检索。如果没有,表单不包括它。

There, if there is a next variable then you include in the url for retrieve it as a get parameter. If not, the form doesn't include it.

这是最好的方法,但您也可以通过请求 POST 字典中的下一个

This is the best approach, but you may also fix this in your views by requesting the next from the POST dictionary like this:

return HttpResponseRedirect(request.POST.get('next'))

请注意,这只会如果模板 account_login 一个变量名为 next 。您应该在视图中生成它,并在呈现模板时将其传递给模板。

Note that this will only work if the template account_login has a variable called next. You should generate it in the views and pass it to the template when you render it.

通常,在模板中,您将执行以下操作:

Normally, in the template you would do something like this:

# this would be hardcoded
next = '/issueapp/1628/view/22'
# you may add some logic to generate it as you need.

然后你做:

return render_to_response(
    'account_login.html',
    {
    'state':state,
    'username': username,
    'next':next
    },
    context_instance=RequestContext(request)
)

希望这有帮助!

这篇关于登录后django重定向不工作“下一个”不张贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆