django - 登录后重定向到另一个页面

标签 django redirect authentication

我使用 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/

相关文章:

python - 如何在模板内部确定图像是否已上传?

redirect - 如何在 Google Apps 脚本中获取位置 header

cookies - 检测用户是否是第一次访问者,如果是,则重定向到页面,如果不是,则重定向到另一个页面

authentication - Keycloak Angular 2 - 检查认证状态Keycloak对象

authentication - 如何从身份验证器重定向到外部身份验证端点?

django - 如何在 Django 中从 html5 和 css3 创建 pdf?

javascript - AJAX 调用后使用 django 变量填充下拉列表

jsp - sendRedirect()可以用作post方法而不是get方法吗? -jsp/servlet

python - 由 Web 应用程序而不是服务器协调的 HTTP 身份验证

python - Django if 语句未按预期工作