django - 登录后如何重定向django.contrib.auth.views.login? [英] django - How to redirect django.contrib.auth.views.login after login?
问题描述
我在我的网页中添加了django.contrib.auth.views.login ,因为我不得不加载一个 templatetag (返回AuthenticationForm)在我的base.html。此模板包括 registration / login.html
模板。
登录状态正常,但我希望将用户重定向到登录前的同一页面。现在,它将我重定向到/ wherever_i_am / login,其中显示 registration / login.html
,使用'login ok'或'login failed'消息,但没有其余的
我遵循了django文档和一些SO问题,如这个< a>但我无法正确重定向。我修改了下一个
变量,但似乎不起作用( next = {{request.get_full_path}}
重新定向到/ wherever_i_am / login ...再次)
你尝试过类似的东西吗?任何想法?
UPDATE1
现在,问题可能是这样的:我必须声明自己的登录查看是否要在我的网页中包含登录表单?
谢谢。
找到答案:
在settings.py中更改settings.LOGIN_REDIRECT_URL
以下代码是从django复制:
if request.method ==POST:
pre>
form = authentication_form(data = request.POST)
如果form.is_valid():
#确保用户发起的重定向网址是安全的。
如果不是is_safe_url(url = redirect_to,host = request.get_host()):
redirect_to = settings.LOGIN_REDIRECT_URL
...
I added django.contrib.auth.views.login everywhere in my webpage, for that I had to load a templatetag (that returns the AuthenticationForm) in my base.html. This templatetags includes the
registration/login.html
template.The login is working ok but I want it to redirect the users to the same page they are before login. Now, it redirects me to /wherever_i_am/login wich shows
registration/login.html
with the 'login ok' or 'login fails' messages but without the rest of base.html.I have followed django documentation and a few SO questions like this but I cannot redirect correctly. I have modified the
next
variable but it doesn't seem to work (next={{ request.get_full_path }}
redirects me to /wherever_i_am/login ...again)Have you tried something similar? any ideas?
UPDATE1 Now, the question could be something like: Do I have to declare my own login view if I want to include the login form everywhere in my web page?
Thank you.
解决方案Found answer:
Change settings.LOGIN_REDIRECT_URL in your settings.py,
below code is copy from django:
if request.method == "POST": form = authentication_form(data=request.POST) if form.is_valid(): # Ensure the user-originating redirection url is safe. if not is_safe_url(url=redirect_to, host=request.get_host()): redirect_to = settings.LOGIN_REDIRECT_URL ...
这篇关于django - 登录后如何重定向django.contrib.auth.views.login?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!