错误405方法不允许,重定向(url_for(' index'))

时间:2018-02-15 13:40:23

标签: flask http-status-code-405

我目前正在为学校项目使用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'))

我的目的是让用户点击按钮并提交他们的详细信息以创建帐户。记录数据后,它们会被重定向回登录页面,该页面位于'索引之下。

在测试完代码后,一切正常,直到重定向部分给出了错误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>

1 个答案:

答案 0 :(得分:0)

您正在使用class XMLNode; namespace std { template<> struct hash<XMLNode> { size_t operator()(const XMLNode & obj) const; }; } class XMLNode { /* actual definition here */ }; size_t std::hash<XMLNode>::operator()(const XMLNode & obj) const { /* actual definition here */ } 。你为什么决定用一段时间作为行动目标?

该操作应为空白,或者只是将其保留以提交给当前网址或<form action="." method="POST">以明确。

因此,简而言之,您将收到错误,因为您的表单提交将转到<form action="{{ url_for('create_account') }}" method="post">,并且该视图仅接受GET请求。

相关问题