方法不允许 - 请求的URL不允许使用该方法

mah*_*mar 1 python flask flask-wtforms flask-restful

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

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

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>
Run Code Online (Sandbox Code Playgroud)

PYTHON方法(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)
Run Code Online (Sandbox Code Playgroud)

Has*_*ood 5

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

所以替换methods=['POST']methods=['POST', 'GET'],你的问题将得到解决.