【问题标题】:Error 405 Method Not Allowed, redirect(url_for('index'))错误 405 方法不允许,redirect(url_for('index'))
【发布时间】:2018-07-26 07:02:59
【问题描述】:

我目前正在将 pythonanywhere 用于学校项目。以下是我遇到问题的一段代码。

#When at www.example.com/ load "log_in.html" file
@app.route("/", methods=["GET"])
def index():
    if request.method == "GET":
        return render_template("log_in.html")

#When at www.example.com/create_account/
@app.route("/create_account", methods=["GET", "POST"])
def create_account():
   if request.method == "GET":
        return render_template("create_account.html", username = username, password = password)

return redirect(url_for('index'))

用户点击按钮并提交详细信息以创建帐户的意图是什么。数据被记录后,会被重定向回'index'下的登录页面。

在测试代码后,一切正常,直到重定向部分给我一个错误 405,方法不允许。

该网站位于http://fishypower.pythonanywhere.com/create_account 单击“创建帐户”按钮将给出错误 405

下面是http://fishypower.pythonanywhere.com/create_account的代码

<html>
    <head>
        <title>ElderMinder: Create Account</title>
    </head>
    <body>
        <div>
            <form action="." method="POST">
                <center><textarea name="username" placeholder="Enter your username"></textarea></center>
                <div></div>
                <center><textarea name="password" placeholder="Enter your password"></textarea></center>
                <div></div>
                <center><input type="submit" value="Create Account"></center>
            </form>
        </div>
    </body>
</html>

【问题讨论】:

    标签: flask http-status-code-405


    【解决方案1】:

    您正在使用&lt;form action="." method="POST"&gt;。您为什么决定使用句点作为行动目标?

    该操作应该是空白的,或者只是将其保留以提交到它的当前 url 或 &lt;form action="{{ url_for('create_account') }}" method="post"&gt; 是明确的。

    因此,简而言之,您会收到错误消息,因为您的表单提交将发送至 /,而该视图仅接受 GET 请求。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-20
    • 2017-11-13
    • 2016-12-02
    • 2012-10-25
    • 2012-03-21
    • 2015-11-25
    • 1970-01-01
    • 2014-10-04
    相关资源
    最近更新 更多