不允许的方法 - 请求的方法不允许 URL

Method Not Allowed - The method is not allowed for the requested URL

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

服务器抛出的问题是"Method Not Allowed"。

HTML CODE

<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 METHOD (submit_article)

@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)

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

所以把methods=['POST']换成methods=['POST', 'GET'],你的问题就解决了。