为了账号安全,请及时绑定邮箱和手机立即绑定

Django重定向登录后无法正常工作“下一步”不发布?

Django重定向登录后无法正常工作“下一步”不发布?

饮歌长啸 2021-03-13 11:08:25
我有一个登录页面,除了重定向到引荐来源页面外,其他都运行正常。用户在应用程序内收到带有直接链接的电子邮件,他们(在此示例中)尚未登录,并被重定向到登录页面。成功登录后,用户将被重定向到硬编码路径。请参见下面的示例。电子邮件中的网址: http://localhost:8000/issueapp/1628/view/22登录页面的URL: http://localhost:8000/login?next=/issueapp/1628/view/22登录视图(带有硬编码的重定向):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)    )登录视图(带有“下一步”重定向):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."上面的视图导致一个异常"Key 'next' not found in <QueryDict: {}>"。即使在url和表单中,该表单似乎也没有发布“ next”变量。我已经搜索并四处张望,无法弄清为什么它不起作用。有任何想法吗?
查看完整描述

3 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

您的代码很好,唯一的问题是,next因为方法是,所以您以表单的形式将属性传递为帖子post。在视图中,您尝试next在get字典中获取显然不存在的参数。


您必须action像这样声明html表单,以便您的视图工作。


{% 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>

在那里,如果有一个next变量,则将其包含在其中url以作为获取参数来进行检索。如果没有,则表单不包含它。


这是最好的方法,但是您也可以通过next从POST字典中请求这样来在视图中解决此问题:


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

请注意,这仅在模板account_login 具有名为的变量时才有效next。您应该在视图中生成它,并在渲染它时将其传递给模板。


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


# 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)

)

希望这可以帮助!


查看完整回答
反对 回复 2021-03-26
?
达令说

TA贡献1821条经验 获得超6个赞

如果您想变得更通用,则可以执行以下操作,在发布表单时,它会传递任何GET参数:


<form action="/path-to-whatever/{% if request.GET %}?{{ request.GET.urlencode }}{% endif %}" method="post">


查看完整回答
反对 回复 2021-03-26
  • 3 回答
  • 0 关注
  • 189 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号