我使用 django 1.10 嵌入式身份验证。我希望登录重定向到另一个页面并传递变量。 我已经发表了我的观点,但我一直有错误 404。我读到了有关“login-redirect-url”的内容,默认是帐户/个人资料。我在模板中创建了一个文件夹。这是正确的方法吗?我将其存储在 Django/myapp/myapp/templates/accounts/profile/home.html
错误:“当前 URL,accounts/profile/home.html,与其中任何一个都不匹配。”
views.py
def home(request):
username = None
if request.user.is_authenticated:
username = request.user.username
return redirect('accounts/profile/home.html',{'username': username})
else:
return redirect('/accounts/profile')
myapp/ulrs.py
url(r'^accounts/profile/$', views.home, name='home')
我更改了settings.py
LOGIN_REDIRECT_URL = '/'
欢迎任何建议。如果我做错了,请给我一个例子。
谢谢。
最佳答案
Dariusz Niespodziany 的答案是有效的,但更好的是更改您的模板。您的 View 将如下所示:
def home(request):
return redirect('/accounts/profile')
然后在你的模板(home.html)中你可以做这样的事情:
{% if request.user.is_authenticated %}
<h1> Hi {{request.user.username}} </h1>
{% else %}
<h1> Hi stranger </h1>
{% endif %}
关于django - 登录后重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40195170/