简体   繁体   中英

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".

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. 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'. The problem with that is that using the updated view which uses user instead of username it redirects to "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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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