【问题标题】:DJANGO - redirect() not redirecting - appends the current path name to the domainDJANGO - redirect() 不重定向 - 将当前路径名附加到域
【发布时间】:2020-10-21 08:13:35
【问题描述】:

基本上,当发出 POST 请求时,我想重定向到另一个页面。

def sample(request): 
    if request.method != "POST":       
        return render(request,"user/sample.html")
    else:
        return redirect("https://www.djangoproject.com")

此代码在收到 GET 请求但在我提交时工作正常 信息,而不是重定向到上面的页面,它将模板名称附加到 url 中,像这样:

http://localhost:8000/sample/sample

无论我在redirect() 中输入什么内容,即使是完全随机的内容,它仍然会重定向到sample/sample

我创建了多个 django 项目,但在每个项目中我仍然遇到这个问题。

【问题讨论】:

  • 你能显示你的url配置和你发布表单的html页面吗?

标签: python django django-forms http-post django-registration


【解决方案1】:

我已经解决了这个问题,把它留给以后的任何人。 我的问题出在 html 文件中

<form action="" method="POST">
    {% csrf_token %}
    {{form}}
<input type="submit">

&lt;form action=""&gt; 必须为空字符串,否则会将其附加到 url。

问题与redirect() 函数无关。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-16
    • 2015-09-14
    • 2020-08-16
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多