Django Redirect to current page

I am having problems redirecting to the same page on which the user was turned on after logging in, no matter how hard I try and read all the questions here. I think the time has come when I showed people my code, so if you can point out the error (s). Thanks.

My login url (in the template base.html): This is present on every page. Changes for logging out after a user logs in. I read that I need to pass a parameter, like "next", like this:

<a href="/login/?next={{request.path}}">Login</a> 

but

* {{request.path}} always empty. *

logon type:

 def mylogin(request): """login view""" try: redirect_to = request.GET.get('next', '/') except ValueError: redirect_to = "/" errors = '' t = loader.get_template('login.html') # check for POST data if request.method == 'POST': username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) # Redirect to a success page. return HttpResponseRedirect(redirect_to) else: c = Context({ 'errors':'disabled', }) return HttpResponse("disabled") else: c = Context({ 'errors':'incorrect', }) return HttpResponse(t.render(c)) else: c = Context({ 'errors':None, }) return HttpResponse(t.render(c)) 

Login form for login.html template

  <form method="post" action="?next={{ redirect_to }}" > <p><label for="username">Username</p> <p></label><input type="text" name="username" value="" id="username" /></p> <p><label for="password">Password</label></p> <p><input type="password" name="password" value="" id="password" /></p> <p><input type="submit" value="Login"></p> </form> 
+4
source share
1 answer

You do not pass request to the template context, so naturally it is empty. The easiest way to get it there is to use RequestContext, which uses embedded context processors to add variables to the context.

The usual way to do this is to use render_to_response with the optional parameter context_instance=RequestContext(request) . For some reason you make all your templates long, so you will need to create this requestcontext yourself.

You also need to add django.core.context_processors.request to the django.core.context_processors.request tuple ins settings.py settings.

+8
source

All Articles