The "next" parameter, redirect, django.contrib.auth.login
Question:
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.
Answers:
create your own view for logging in, with it’s own url, don’t use the admin’s one.
you can store the next page in the session, or pass it as a GET parameter to the login view
(i.e. /login?next=gallery) just don’t forget to sanitize and validate that value before redirecting to it.
If you already have the custom template for login form you need to add the following inside your <form>
tag:
<input type="hidden" name="next" value="{{next}}" />
BTW, you don’t have to create your own login view. django.contrib.auth.views.login works fine. You only need to create a template for it (registration/login.html
)
You can use a static redirect to /loggedin/
and then associate the url to a view that makes the correct redirect.
Login takes an extra step but if you want to use django’s view it does the job.
I confess I usually use 2 redirects in order to get something like this to work.
First, Make your own registration/login.html
page. You can copy-and-paste the html example in this section of the authentication docs to make the process a little easier. Instead of using the dynamic '{{ next }}
variable from the context, however, hardwire the value of next to go to a generic landing view of logged-in users
<input type="submit" value="login" />
<input type="hidden" name="next" value="/gallery/" />
Then, in the view that you map to the /gallery/
URL, extract the User object from the request (since the user will now be logged in, especially if the gallery view is wrapped in a @permission_required
or @login_required
decorator. Use that view to redirect to the appropriate user-specific gallery page:
@login_required
def gallery(request):
url = '/gallery/%s/' % request.user.username
return HttpResponseRedirect(url)
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.
being an newbie to django and stumbling over this somewhat older thread i found a differing solution for the problem of dynamically (=override a custom default only if needed) setting the next-param that i’d like to share (working fine with django 1.5, earlier versions untested):
just as django-d i wanted avoid repetition and a custom login-view, so i used the stock django.contrib.auth.views.login
-view by adding the line of
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html',}, name='login'),
to my urls.py and within the login.html-templates form-element:
{% if not next or not next.strip %}
{# avoid django.contrib.auth.views.login s default of /account/profile/ #}
{% url 'afterlogindefaultview' as next %}
{% endif %}
<input type="hidden" name="next" value="{{ next }}" />
which to my understanding follows the decoupling-practice of the url-configurations from the views.
so for views that should redirect to my apps login and afterwards head to a non-default view
i use
return HttpResponseRedirect('%s?next=%s' % (reverse('login'), reverse('mycustomnext')) )
from the view where i want to have the user to log in. i use this to get back to the view where i left off for logging the user in.
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.
create your own view for logging in, with it’s own url, don’t use the admin’s one.
you can store the next page in the session, or pass it as a GET parameter to the login view
(i.e. /login?next=gallery) just don’t forget to sanitize and validate that value before redirecting to it.
If you already have the custom template for login form you need to add the following inside your <form>
tag:
<input type="hidden" name="next" value="{{next}}" />
BTW, you don’t have to create your own login view. django.contrib.auth.views.login works fine. You only need to create a template for it (registration/login.html
)
You can use a static redirect to /loggedin/
and then associate the url to a view that makes the correct redirect.
Login takes an extra step but if you want to use django’s view it does the job.
I confess I usually use 2 redirects in order to get something like this to work.
First, Make your own registration/login.html
page. You can copy-and-paste the html example in this section of the authentication docs to make the process a little easier. Instead of using the dynamic '{{ next }}
variable from the context, however, hardwire the value of next to go to a generic landing view of logged-in users
<input type="submit" value="login" />
<input type="hidden" name="next" value="/gallery/" />
Then, in the view that you map to the /gallery/
URL, extract the User object from the request (since the user will now be logged in, especially if the gallery view is wrapped in a @permission_required
or @login_required
decorator. Use that view to redirect to the appropriate user-specific gallery page:
@login_required
def gallery(request):
url = '/gallery/%s/' % request.user.username
return HttpResponseRedirect(url)
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.
being an newbie to django and stumbling over this somewhat older thread i found a differing solution for the problem of dynamically (=override a custom default only if needed) setting the next-param that i’d like to share (working fine with django 1.5, earlier versions untested):
just as django-d i wanted avoid repetition and a custom login-view, so i used the stock django.contrib.auth.views.login
-view by adding the line of
url(r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html',}, name='login'),
to my urls.py and within the login.html-templates form-element:
{% if not next or not next.strip %}
{# avoid django.contrib.auth.views.login s default of /account/profile/ #}
{% url 'afterlogindefaultview' as next %}
{% endif %}
<input type="hidden" name="next" value="{{ next }}" />
which to my understanding follows the decoupling-practice of the url-configurations from the views.
so for views that should redirect to my apps login and afterwards head to a non-default view
i use
return HttpResponseRedirect('%s?next=%s' % (reverse('login'), reverse('mycustomnext')) )
from the view where i want to have the user to log in. i use this to get back to the view where i left off for logging the user in.