redirect - django return to index when there is no next= in URL -


I created a login, which would have to redirect to the previous page (with /? Next = /) that started working , But when there is no next = it remains on the login page without redirecting to the index. Where should I make my mistake?

  DR login_user (request): login_form = loginForm (request.POST or none) if request.POST and login_form.is_valid (): user = login_form.login (request) if user: login (Request, user) HTTP response redirect (request.POST.get ('next', reverse ('index')) #Rarton HTTP response redirect (reverse ('index')) submit the submission (request, login '.html Django ('next', '')})  .conf.urls Import Pat From the URL import url pattern = pattern ('', '', #ifification url url (r '^ login / $', views.login_user, import name)  
  = 'Login_user'), Url (r '^ logout / $', views.logout, name = 'logout'), url (r '^ register / $', views.register_user, name = 'register_user'),)   

urlpatterns = pattern ('', url (r '^ $', views.index, name = 'index'), my urls.py (in my case AP)

  )   

My template: (yes it's a bit of work, not a bit old piece)

  {% block content%} & lt; Div & gt; & Lt; Form action = "{% url 'login_user'%}" method = "post" class = "login" & gt; {% csrf_token%} & lt; Table & gt; {% If login_form.non_field_errors%} & lt; Tr & gt; & Lt; Td> & Lt; I style = "font-size: small;" & Gt; {{Login_form.non_field_errors; Striipats}} & lt; / I & gt; & Lt; / Td> & Lt; / TR & gt; {% Endif%} for the field in {% login_form%} & lt; Tr & gt; & Lt; Td> {{Field}} & lt; / Td> & Lt; / TR & gt; {% Endfor%} & lt; Tr & gt; & Lt; Td style = "text-line: true;" & Gt; & Lt; A href = "{% url 'register_user'%} '& gt; Register & lt; / a & gt; & Amp; nbsp; & nbsp; & amp; nbsp; & nbsp; & lt; Input type =" submit "Value =" login "/>     input type =" hidden "name =" next "value = "{{Next}}">   {% Endblock%}    

The problem is that if you have the GET parameter in the specified code> next parameter, it will be an empty string, but It will still be defined as request.POST.ge T ('next') will never fail, but only return an empty string you can do the following:

  def login_user (requested): login_form = LoginForm ( request.POST or none) if request .post and login_form.is_valid (): user = login_form.login (request) if user: login (request, user) httpresppspoint redirect (request.post.get ('next') or reverse ('Index')) #Return HttpResponseRedirect (reverse ('index')) submit the submission (request, 'login. Html ',' login_form ': login_form,' next ': request.GET.get (' next ',' ')})   

will return the value of , Or reverse ('index') if that value is empty

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -