11

I go to my webpage http://localhost:8000/listings/post/, it fails the test

@user_passes_test(lambda u: u.is_authenticated() and u.get_profile().shipper)

as expected, and redirects me to http://localhost:8000/login/?next=/listings/post/ like it's supposed to, but when I log in again, it doesn't redirect me back to that page like it's supposed to. It takes me to /accounts/profile/. I haven't defined redirect_field_name anywhere, so it should be looking for the default next variable. The relevant urls.py bit looks like this

url(r'^login/$', 'django.contrib.auth.views.login', name='login'),

So what are the possible causes for this?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

2 Answers2

17

Needed to add

<input type="hidden" name="next" value="{{ next }}" />

To my login form. It was posting back to the login URL without the next token, and then trying to redirect.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • 1
    So I was right in the end, your next value was not set, that's why it redirected you to LOGIN_REDIRECT_URL. – stefanw Feb 15 '10 at 18:07
  • 2
    Well...okay, fine. You can have the check then. I thought it was set because it was appearing in the URL. But you still didn't really solve the problem ;) – mpen Feb 16 '10 at 02:06
  • Which form did you need to add this to? – Lynden Shields Dec 03 '12 at 00:06
  • @LyndenShields: Login form I presume. – mpen Dec 03 '12 at 01:17
  • 1
    Thanks, I was having a lot of trouble figuring out why it wasn't working - I found the built in login form and it already had this but the redirect still wasn't working. Eventually copied it to a folder inside my site's templates directory called registration. Then it started working. Took me a long time to find the right place for it. Django Debug Toolbar helped me find out that it was the location of the template that was wrong that was causing the problem. – Lynden Shields Dec 03 '12 at 01:46
4

It redirects you to the settings variable LOGIN_REDIRECT_URL.

This happens when the following is true:

if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL

Most probably your 'next' variable is empty or contains garbage.

stefanw
  • 10,456
  • 3
  • 36
  • 34
  • 1
    Uh....no. As I said, I get sent to `http://localhost:8000/login/?next=/listings/post/`. `next` contains the proper value. – mpen Feb 15 '10 at 01:02