"Not Found" 将照片上传到网站并在 flask 上提交时出错 python

"Not Found" Error when uploading photo to website and submitting on flask python

我正在尝试创建允许用户输入照片并将照片保存到文件夹“/home/HassanSherien/mysite/Shape_Image”的代码。我已按照下面 link 中给出的步骤进行操作,但我不断收到一条错误消息 "Not Found, the requested URL was not found on the serves. if you entered the URL manually, please check your spelling and try again."

Python:

 import os
 from flask import Flask, request, redirect, url_for
 from werkzeug.utils import secure_filename
UPLOAD_FOLDER = '/home/HassanSherien/mysite/Shape_Image'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def upload_file():
    if request.method == 'POST':
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)
    file = request.files['file']
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)
    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

HTML:

<form action="myform.cgi"> 
<input type="file" name="fileupload" value="fileupload" 
id="file`upload"> 
<label for="fileupload"> Select a file to upload</label>
 <br>
<input type="image" src="/wp-content/uploads/sendform.png" 
alt="Submit" width="100"> 
</form>

我也在线上报错 如果文件和 allowed_file(file.filename): 那说 "undefined name 'allowed_file'"

Upload image in Flask

您还没有为您的 upload_file 函数分配路由。这应该在函数定义上方的行上完成:

@app.route('/upload', methods=['POST'])
def upload_file():
    # rest of your code

此外,您还需要使表单的操作与此相对应 URL:

<form action="/upload" method='POST'>

在 Flask 模板中,您可以根据函数名称自动生成 URL:

<form action="{{ url_for('upload_file') }}" method='POST'>

此外,您表单中唯一的输入字段应该是:

<input type="file" name="file" id="file_upload" /> 
<input type="submit />

请注意,我设置了 name="file",它对应于您的 Python 代码接受的内容。请参阅 this answer 我刚刚发布的原因。关于这个的官方文档有点令人困惑,因为他们在多个地方重复使用术语 'file',我认为这会让人们感到困惑,尤其是新手。


I am also given an error on the line if file and allowed_file(file.filename): that says "undefined name 'allowed_file'"

听起来您没有按照文档定义该函数:

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

根据评论编辑

now my website will allow people to upload photos and it will automatically submit (since despite there being no submit button the code knows to right after something is uploaded submit it)

抱歉:提交按钮需要第二个输入标签:就在表单末尾 <input type='submit' />

一旦用户选择了他们想要上传的文件,他们就会点击这个按钮,一个 POST 请求将连同数据一起发送到服务器。

to url_for('upload_file') which was '/home/HassanSherien/mysite/Shape_Image' in my case.

当表单实际呈现时(在任何上传 activity 之前)表单的 action 属性将是 {{url_for('file_upload')}} 的 return 值这将是 /upload(在 @app.route 行中设置)。这使得表单提交到:http://example.com/upload。不要将此与服务器端 UPLOAD_FOLDER 中输入的路径混淆。

在处理上传的代码中,以下行将指定为 UPLOAD_FOLDER 的路径连接到 filename 的值:

file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

为了更容易阅读,它与以下内容相同:

full_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(full_path)

第二行是调用 file 对象的 save 方法,提供完整路径作为参数。这实际上是将文件保存到磁盘。

如果您发现这没有发生,请检查服务器控制台,因为您可能需要设置目录权限等。

此功能还应该 return 类似于重定向到网站其他地方的功能。