亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號