7π
the login_required
decorator annotates the login-url adding a next
field to the GET parameters. see login_required.
e.g. if LOGIN_URL
is /login
it becomes /login?next=/polls/3/vote
This gets passed to your login view, where you use it
-
To render the login page upon GET or errorneos POST in a request aware manner. Please note that you need to pass the request variables to the template rendercontext. see RequestContext and render_to_response.
return render_to_response( 'login.html', my_data_dictionary, context_instance=RequestContext(request) )
-
in the template add a line like this to the login form
<input type="hidden" name="next" value="{{ next }}">
-
Finally upon a succesfull POST use this value to
HttpResponseRedirect
, either to the value ofnext
or toLOGIN_REDIRECT_URL
if nonext
value is present. see HttpResponseRedirect.... user was succesfully authenticated... if request.POST["next"] is Not "": HttpResponseRedirect(request.POST["next"]) else: HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
I hope this clarifies things a little bit.
1π
I got it to work doing the following:
def 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)
return HttpResponseRedirect(request.POST.get('next', reverse('index')))
return render(request, 'login.html', {'login_form': login_form, 'next': request.GET.get('next', '') })
#return render_to_response('login.html', {'login_form': login_form, 'next': request.GET.get('next', '')}, context_instance=RequestContext(request))
also included
<input type="hidden" name="next" value="{{ next }}">
in my login.html login_form
- [Django]-Serving static files for development mode in Django
- [Django]-How to configure code completion for Django based projects in PyDev?
- [Django]-URL issue when using lighttpd, django and fastcgi