How to use 'user.username' in the settings.py
I'm having trouble with my URL forwarding after successful login.
开发者_运维知识库The django default for 'LOGIN_REDIRECT_URL' is '/accounts/profile/'. In my case it should forward to '/user/{{ user.username }}/'
I went to overwrite it in my settings file but have encountered a syntax issue. Here's the code:
LOGIN_REDIRECT_URL = "/user/%s/" % user.username
When I use the following line in my login form, it works, but only if the page is reloaded first. Kinda strange.
<input type="hidden" name="user" value="/user/{{ user.username }}/" />
So what changes must I make to my settings.py to load the username on redirect?
Thanks.
You may want to create a custom login instead and use the views described in django user authentication.
Of course overriding the view where /accounts/profile/ points to and manually setting the redirect url there is an option, but its not a very elegant solution.
Of course there's a syntax issue, you're referencing a variable, user
, in settings.py that doesn't exist there. It might help you to go through a Python tutorial to understand Python's scoping rules.
In any case, settings.py is for static global settings. Usernames obviously vary per user, so you can't put a dynamic setting there.
精彩评论