gpt4 book ai didi

django - 登录后重定向到最后一个 View

转载 作者:行者123 更新时间:2023-12-02 00:49:56 25 4
gpt4 key购买 nike

我已经和这个问题斗争了一段时间,并用谷歌搜索并浏览了文档,所以我想是时候问一下了。我正在尝试让我的应用程序在登录后重定向到上次查看的页面。我正在运行 django 1.2.4,但到目前为止还没有运气。

这个堆栈溢出线程似乎可以解决问题,但我还没有成功: Django: Redirect to previous page after login ...

目前,从任何 View 登录后,我都会被重定向到://localhost:1100/accounts/profile/

settings.py 有以下建议代码: “django.core.context_processors.request”,

将此作为我的登录按钮链接: <a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">login</a>

我还确保在 view.py 文件中导入 RequestContext: 从 django.template 导入 RequestContext

我的印象是这不起作用。我现在也注意到了登录 URL 中包含部分下一个 URL://localhost:1100/accounts/login/?next=

建议?提前致谢!

最佳答案

正如sdolan所说,您可以使用login_required装饰器,但还有更多的可能性:

  • 表单操作:

    <form method="post" action="{% url django.contrib.auth.views.login %}?next={{request.path}}">
  • 下一个隐藏字段:

     <input type="hidden" name="next" value="{{request.path}}" />
  • 带有登录表单的链接:

    <a href="{% url django.contrib.auth.views.login %}?next={{request.path}}">Login</a>

要使用它们,您必须在相应 View 中传递请求上下文。可以在此处找到执行此操作的示例:http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

关于django - 登录后重定向到最后一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4600762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com