簡體   English   中英

django登錄裝飾器和用戶重定向問題

[英]django login decorator and user redirect issues

這是我需要做的。 我有一個名為“賣出”的按鈕。 如果用戶已登錄,則它會執行其正常步驟並允許用戶進行銷售。 如果用戶未登錄,則需要重定向至登錄頁面,並在用戶登錄后,將登錄用戶執行的正常步驟重定向至帶有用戶名和用戶名的url,然后以這種“用戶名/銷售方式”進行銷售。

該視圖作為經過身份驗證的用戶運行良好

@login_required()
def UserSell(request,username):

    thegigform=GigForm()
    theuser=User.objects.get(username=username)
    if request.method=='POST':
         gigform=GigForm(request.POST,request.FILES)
          if gigform.is_valid():
        gigform.title=gigform.cleaned_data['title']
        gigform.description=gigform.cleaned_data['description']
        gigform.more_info=gigform.cleaned_data['more_info']
        gigform.time_for_completion=gigform.cleaned_data['time_for_completion']
        #need to change this, shouldnt allow any size image to be uploaded
        gigform.gig_image=gigform.cleaned_data['gig_image']
        #commit=False doesnt save to database so that I can add the current user to the gig
        finalgigform=gigform.save(commit=False)
        finalgigform.from_user=theuser
        finalgigform.save()
        return HttpResponseRedirect('done')

else:
    gigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':gigform},context_instance=context)

這是網址

url(r'^(?P<username>\w+)/sell$','gigs.views.UserSell', name='sell'),

然后模板

<a href="{% url sell user.username %}"><button type="button">Start Selling!</button></a>

現在這很有效,因為我是登錄用戶,然后以匿名用戶的身份在另一個瀏覽器上嘗試使用時,我很快看到匿名用戶沒有用戶名,因此我更改了視圖,URL和模板以僅使用用戶。 然后工作正常,直到裝飾器重定向到登錄頁面后嘗試登錄。 登錄后的{{next}}網址是絕對路徑,即“用戶/出售”。 這樣做的問題是,使用使用用戶而不是用戶名的更新視圖將其重定向到“ AnonymousUser / sell”。 我認為這與我的觀點有關,但有人可以提供幫助。 我需要登錄后的重定向為“用戶/銷售”,就像最近登錄的用戶一樣。

我認為您不必輸入用戶名,因為當用戶登錄時,系統現在可以訪問其用戶名。

@login_required()
def UserSell(request):
    ..........

    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('app_name:login'))
    else:
        context=RequestContext(request)
        return render_to_response('sell.html',{'theuser':request.user,'thegigform':gigform},context_instance=context)

如果要顯示用戶,則只需輸入“ request.user”或“ request.user.username”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM