Django after @login_required redirect to next

后端 未结 4 1178
耶瑟儿~
耶瑟儿~ 2020-12-25 08:46

I feel like this is an easy question and I\'m just missing 1 small step.

I want to do any number of the following (as the term in the next parameter):



        
4条回答
  •  忘掉有多难
    2020-12-25 09:17

    What I have ended up doing is the following. To me this seems like a bit of a hack job. Is there any better way to use login with a csrf?

    views:

    def login(request):
      c={}
      c.update(csrf(request))
      if 'next' in request.GET:
        c['next'] = request.GET.get('next')
      return render_to_response('login.html', c)
    
    def auth_view(request):
      username = request.POST.get('username', '')
      password = request.POST.get('password', '')
      user = auth.authenticate(username=username, password=password)
    
      if user is not None:
        auth.login(request, user)
    
        if request.POST.get('next') != '':
          return HttpResponseRedirect(request.POST.get('next'))
        else:
          return HttpResponseRedirect('/accounts/loggedin')
      else:
        return HttpResponseRedirect('/accounts/invalid')
    

    login.html:

    
    

提交回复
热议问题