如果您可以redirect_to进入登录模板的上下文,那么您可以使用一种机制来选择不同的重定向 URL。
在您的登录模板中,向表单添加一个额外的隐藏输入
<input type="hidden" name="next" value="{% url redirect_to %}">
现在在您的 urls.py 文件中,您可以指定用于重定向的输入:
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html',
'redirect_field_name': 'next'},
name='auth_login'),
如果你这样做,那么你将被重定向到不同的地方,具体取决于redirect_to.
您可以redirect_to通过拥有多个命名登录 url 来进入您的上下文:
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html',
'redirect_field_name': 'next',
'extra_context': {'redirect_to': 'foo_url'}
},
name='foo_login'),
url(r'^login/$',
auth_views.login,
{'template_name': 'registration/login.html',
'redirect_field_name': 'next',
'extra_context': {'redirect_to': 'bar_url'}
},
name='bar_login'),
或者如果你不想做那种事情,你可以在会话中使用一些东西:
<input type="hidden" name="next" value="{% url session.redirect_to %}">
希望这会有所帮助,如果代码中存在拼写错误,我们深表歉意!auth login 视图的文档很难链接到link。转到该链接并向上滚动一下!