简体   繁体   English

请求的 URL 不允许使用方法

[英]Method not allowed for requested URL

I am trying to upload file from my flask site,but it keeps returning the error我正在尝试从我的 flask 站点上传文件,但它一直返回错误
method is not allowed for the requested URL . method is not allowed for the requested URL Even my teacher does not have the answer to this question.就连我的老师也没有这个问题的答案。 According to him he has never seen this error.据他说,他从未见过这个错误。 really appreciate your help非常感谢您的帮助

my HTML file is as follows我的 HTML 文件如下

<!DOCTYPE html>
<html lang="en">
  <title> Data Collector App </title>
  <head>
    <link href="../static/main.css" rel="stylesheet">
  </head>
    <body>
      <div class="container">
        <h1>Data Collector</h1>
        <form action={{url_for('index')}} method="POST" enctype="multipart/form-data">
          <input type="file" name="file">
          <button type="submit">Submit</button>
        </form>
      </div>
    </body>
</html>

Python srcipt is Python 脚本是

from flask import Flask, render_template, request, send_file, url_for
import pandas
from werkzeug.utils import secure_filename

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/index')
def upload():
    if method == "POST":
        file=request.files['file']
        file.save(secure_filename("new"+file.filename))
        return render_template('index.html')

if __name__ == "__main__":
    app.run(debug = True)

Add any allowed methods to a route in the decorator, eg将任何允许的方法添加到装饰器中的路由,例如

@app.route('/index', methods=['POST', ...])

EDIT: You should probably also check on the method field of request instead of just method .编辑:您可能还应该检查requestmethod字段,而不仅仅是method

if request.method == 'POST':

By default routes only accept the GET method.默认情况下,路由只接受GET方法。 If you want your route to answer to other methods, pass a custom methods parameter to @app.route as follows如果您希望您的路由响应其他方法,请将自定义methods参数传递给@app.route ,如下所示

@app.route('/', methods=['GET', 'POST',])
...

@app.route('/index', methods=['GET', 'POST',])
...

Source https://flask.palletsprojects.com/en/1.1.x/quickstart/#http-methods来源https://flask.palletsprojects.com/en/1.1.x/quickstart/#http-methods

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM