【问题标题】:Method Not Allowed - The method is not allowed for the requested URL [duplicate]方法不允许 - 请求的 URL 不允许该方法 [重复]
【发布时间】:2017-01-10 09:55:12
【问题描述】:

我正在尝试构建一个 python 烧瓶应用程序,但是当我尝试将表单数据提交给 python 方法时遇到了问题。

服务器抛出的问题是“方法不允许”。

HTML 代码

<h1>Submit the Link</h1>
    <form action="/submit_article" method="POST" name="form">
        <div class="col-md-4 col-md-offset-4">
            {{ form.hidden_tag() }}
            <div class="form-group">
                <label class="control-label" for="description">Name</label>
                {{ form.description }}
            </div>
            <div class="form-group">
                <label class="control-label" for="link">Link</label>
                {{ form.link }}
            </div>
            <button class="btn btn-default" type="submit">Submit</button>
        </div>
    </form>

PYTHON 方法(提交文章)

@app.route('/submit_article', methods=['POST'])
def submit_article():
  form = UploadForm()
  if request.method == 'POST':
    data = {
        "_id": form.link.data,
        "description": form.description.data,
        "user": current_user.username,
        "time": datetime.datetime.now()
    }

    try:
        if((app.config['NEWS_COLLECTION'].insert(data))):
            flash("Link added successfully", category="success")
            return redirect(request.args.get("new") or url_for("new"))

    except DuplicateKeyError:
        flash("Article link already exists in the Database", category="error")
        return render_template("submit_article.html")

  return render_template('submit_article.html', title='login', form=form)

【问题讨论】:

    标签: python flask flask-wtforms flask-restful


    【解决方案1】:

    该方法不被允许,因为您在methods 列表中只指定了“POST”。但是,每当您尝试访问此 URL 时,它都会发送“GET”请求。当您单击按钮时将发送“POST”请求,但最初它将通过“GET”请求访问页面。

    因此,将methods=['POST'] 替换为methods=['POST', 'GET'],您的问题将得到解决。

    【讨论】:

    • 感谢@Hassan Mehmood,它成功了。 :)
    猜你喜欢
    • 2016-12-12
    • 2014-01-08
    • 2020-07-30
    • 2021-08-16
    • 2016-04-23
    • 2023-03-09
    • 1970-01-01
    • 2021-11-13
    • 2020-03-05
    相关资源
    最近更新 更多