Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The "next" parameter, redirect, django.contrib.auth.login

I'm trying to redirect users to custom url "/gallery/(username)/" after successfully logging in. It currently redirects to the default "/account/profile/" url While I know what I can override the redirect url in my settings.py, my url is dynamic thus it will not work.

Documentation states that I need to use the "next" parameter and context processors. I have the {{next}} in my template, but I'm confused on how to actually pass the "/gallery/(username)". Any help would be greatly appreciated.

p.s: I'm trying to steer away from writing my own login view.

like image 339
django-d Avatar asked Aug 09 '10 15:08

django-d


People also ask

What is next URL in Django?

The next parameter appears in a URL when a user is trying to get to a page but is redirected to another page first before being able to access that page. This may occur such as when a view requires a login through a login_required decorator.

How do I authenticate username and password in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...


1 Answers

Django's login view django.contrib.auth.views.login accepts a dictionary named extra_context. The values in the dictionary are directly passed to the template. So you can use that to set the next parameter. Once that is done, you can set a hidden field with name next and value {{ next }} so that it gets rendered in the template.

like image 172
Guruprasad Avatar answered Sep 20 '22 00:09

Guruprasad