views:

239

answers:

4

I'm trying to redirect users to custom url "/gallery/(username)/" after successfully logging in. It currently redirects to the default "/account/profile/" 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.

A: 

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.

ozk
I really don't want to create a custom login view, but even if I did your example doesn't show how to pass the username. How Is "/login?next=gallery" supposed to automatically redirect to /gallery/username. Also where am I supposed to place "/login?next=gallery" within the URLconf or within the view using a regex?
django-d
the username is available in the session data.the URLconf part maps '/login' to your view. the ?next=gallery will be available in your view in request.GET['next']
ozk
A: 

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)

Andrey Fedoseev
This is exactly how I have it set up, but where exactly do I assign the value for "next". And how do I specify a dynamic value for "next".
django-d
I see now, you want a username in {{next}} and you don't know it before the form is submitted. In that case you have to create your own login view. Another option is to use a little of Javascript to fill the 'next' field right before the form is submitted.
Andrey Fedoseev
+1  A: 

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.

naw
+1  A: 

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)
Jarret Hardie