简体   繁体   English

django登录装饰器和用户重定向问题

[英]django login decorator and user redirect issues

This is what i need done. 这是我需要做的。 I have a button called sell. 我有一个名为“卖出”的按钮。 If the user is logged in then it goes through its normal steps and allows for the user to sell. 如果用户已登录,则它会执行其正常步骤并允许用户进行销售。 If the user is not logged in I need to redirect to the login page and after the user logs in, redirect down the normal steps a logged in user takes which ia url with the users username and sell like this "username/sell". 如果用户未登录,则需要重定向至登录页面,并在用户登录后,将登录用户执行的正常步骤重定向至带有用户名和用户名的url,然后以这种“用户名/销售方式”进行销售。

This was the view that worked fine as a authenticated user 该视图作为经过身份验证的用户运行良好

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

here is the urls 这是网址

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

then template 然后模板

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

Now this worked great as i was a logged in user, then when i tried it on another browser as an anonymous user i quickly saw that an anonymous user doesn't have a username so i changed the view, url, and template to use just user. 现在这很有效,因为我是登录用户,然后以匿名用户的身份在另一个浏览器上尝试使用时,我很快看到匿名用户没有用户名,因此我更改了视图,URL和模板以仅使用用户。 Then that worked fine until trying to login after the decorator redirected to the login page. 然后工作正常,直到装饰器重定向到登录页面后尝试登录。 The {{next}} url after login is the absolute path which is 'user/sell'. 登录后的{{next}}网址是绝对路径,即“用户/出售”。 The problem with that is that using the updated view which uses user instead of username it redirects to "AnonymousUser/sell". 这样做的问题是,使用使用用户而不是用户名的更新视图将其重定向到“ AnonymousUser / sell”。 I think its a issue with my view but can someone please help. 我认为这与我的观点有关,但有人可以提供帮助。 I need the redirect after login to be "user/sell" as in the recently logged in user. 我需要登录后的重定向为“用户/销售”,就像最近登录的用户一样。

I think you don't have to required username because when the person login, the system can now access their username. 我认为您不必输入用户名,因为当用户登录时,系统现在可以访问其用户名。

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

If you want to show the user just put "request.user" or "request.user.username" 如果要显示用户,则只需输入“ request.user”或“ request.user.username”

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

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