2

In this section of the Django documentation, it says that passing in redirect_field_name via the url conf, as in

url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'redirect_field_name': '/albums/all/'}),")

should override the next value, which is where it goes after a successful login (it's stated between the two green boxes).

Mine keeps going to the default (settings.LOGIN_URL, which is "/accounts/profile/", and I don't understand why.

Printing next in the login template results in the empty string: <P>next={{ next }}</P>

Temporarily giving up on this, I eliminated the {'redirect_field_name', ... from that line in urls.py, and instead changed LOGIN_URL in settings.py to /albums/all/. I restarted the server, yet it's still going to /accounts/login.

I see this question, but there's no reference to urls.py: Django- why inbuilt auth login function not passing info about user to after successful login url

What am I missing?

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108

1 Answers1

4

From what I understand from the documentation, redirect_field_name is used as a key to GET, where the value is the url to redirect to. Looking at the source here also suggests the same. If this is the case redirect_field_name shouldn't be set to a url. According to the documentation for login_required further up in the link you sent, next is passed to the view in the query string. Try passing in the url you wish to redirect to there.

crhodes
  • 1,178
  • 9
  • 20
  • Thank you for the help @crhodes. I don't understand how you mean to pass in this url to the view. How else to do it, except via its line in `urls.py`? This is how the documentation says to pass in the `template_name` parameter to the views.login() function (as in the source code you linked to): `(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'myapp/login.html'}),`. Since another parameter in that same function is `redirect_field_name`, I thought using the same method of passing would work, but obviously it doesn't. Can you please elaborate on what your suggesting? – aliteralmind Aug 15 '14 at 18:39
  • From what I understand there are two options, either you specify the url in the querystring like so: `?next=/albums/all/`. Or, you specify a `redirect_field_name` and customize the login template as the docs note: `Note that if you provide a value to redirect_field_name, you will most likely need to customize your login template as well, since the template context variable which stores the redirect path will use the value of redirect_field_name as its key rather than "next"` – crhodes Aug 15 '14 at 18:49
  • I understand the manually adding `?next=...` to the url. I don't know how to "specify a `redirect_field_name`" As far as customizing the template, I can only guess that it means changing `` to `...name="next" value="{{ redirect_field_name }}" />`. If you would be willing to continue this in chat, it would be helpful. – aliteralmind Aug 15 '14 at 19:00
  • If you change the `redirect_field_name` the template would change as you suggested. However I believe it would be `input type="hidden" name="{{ redirect_field_name }}" value="/albums/all/" />`. If you look at the source I linked the `redirect_to` variable storing the url is first checked for in the POST to see if the `redirect_field_name` is there, otherwise it falls back on the GET and the url being in the querystring. – crhodes Aug 15 '14 at 19:23
  • Oh! It's a *name*. I don't care about the name! I want the *value* to be `/albums/all/`. Right? Well that certainly clears things up a bit. So I could just forget about this `redirect_field_name` variable entirely, and just hard-code the value right into the template. That doesn't seem like "the right way" to do it, but it's a step in the right direction. (I wrote `name="next" value="{{ name }}"/>` in my previous comment, should be `value="{{ next }}"`). Let me give it a try! – aliteralmind Aug 15 '14 at 19:28
  • This works: `` Thank you for the helping me through this, @crhodes. I've been banging my head on this for four hours. PHEW. – aliteralmind Aug 15 '14 at 19:32