Flask 奇怪行为 w/ 文件夹 Creation/File 正在上传
Flask Odd Behavior w/ Folder Creation/File Uploading
首先我会说我是 Flask 的新手(这是我的第一个项目)并且我有兴趣一起破解它,而不是最佳实践。
我目前的代码无法在图片目录中创建用户命名的文件夹。我已经尝试在这里寻找一些答案,但无济于事,我能否让所有这 3 件事协调一致。这是有问题的功能。
@app.route('/', methods = ["GET","POST"])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form.get('name')
if not os.path.exists("/pictures/directory"): os.makedirs("/pictures"+foo)
app.config["UPLOAD_FOLDER"] = "/pictures" + foo
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
如果有人有兴趣也看看为什么 upload.html 首先呈现(这是预期的)但 "Continue" 按钮无法呈现 index.html,我会非常感激。
如果有人对它的其余部分感到好奇,这里是回购协议:https://bitbucket.org/dillon9/ss
编辑 1:感谢你们两位,我拥有了一个半功能的前端和一个功能齐全的后端。推送新代码。希望我能接受你的两个答案
这是因为您的 foo
变量没有保存您的用户提供的值。您首先获取用户使用
指定的名称
foo = request.form.get('name')
但是你在使用它之前立即将其他东西分配给同一个变量
foo = "/directory/"
编辑:现在您的目录可能正在 C:\ 或其他目录中创建。把你的代码改成这样
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form['name']
path = os.path.dirname(os.path.abspath(__file__)) + "/pictures/"+foo
if not os.path.exists(path):
os.makedirs(path)
app.config["UPLOAD_FOLDER"] = path
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
您的代码中有几处需要更改。
第一个:
通常根页面 "/"
被映射到一个 index
命名函数。
@app.route('/', methods = ["GET","POST"])
def index():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form.get('name')
if not os.path.exists("/pictures/directory"):
os.makedirs("/pictures"+foo)
app.config["UPLOAD_FOLDER"] = "/pictures" + foo
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
第二:
使用单个按钮(在本例中为 Update
)更新内容和重定向更有意义,然后您可以丢弃 Continue
按钮。
第三名:
在您的 upload.html 文件中,您必须更正表单代码
<form action="" method=post enctype=multipart/form-data>
到
<form action="{{ url_for("index") }}" method= "post" enctype= "multipart/form-data">
因此,您将处理此表单的函数的 url 作为值赋予 action
属性。最后,在值周围添加双引号。
首先我会说我是 Flask 的新手(这是我的第一个项目)并且我有兴趣一起破解它,而不是最佳实践。
我目前的代码无法在图片目录中创建用户命名的文件夹。我已经尝试在这里寻找一些答案,但无济于事,我能否让所有这 3 件事协调一致。这是有问题的功能。
@app.route('/', methods = ["GET","POST"])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form.get('name')
if not os.path.exists("/pictures/directory"): os.makedirs("/pictures"+foo)
app.config["UPLOAD_FOLDER"] = "/pictures" + foo
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
如果有人有兴趣也看看为什么 upload.html 首先呈现(这是预期的)但 "Continue" 按钮无法呈现 index.html,我会非常感激。
如果有人对它的其余部分感到好奇,这里是回购协议:https://bitbucket.org/dillon9/ss
编辑 1:感谢你们两位,我拥有了一个半功能的前端和一个功能齐全的后端。推送新代码。希望我能接受你的两个答案
这是因为您的 foo
变量没有保存您的用户提供的值。您首先获取用户使用
foo = request.form.get('name')
但是你在使用它之前立即将其他东西分配给同一个变量
foo = "/directory/"
编辑:现在您的目录可能正在 C:\ 或其他目录中创建。把你的代码改成这样
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form['name']
path = os.path.dirname(os.path.abspath(__file__)) + "/pictures/"+foo
if not os.path.exists(path):
os.makedirs(path)
app.config["UPLOAD_FOLDER"] = path
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
您的代码中有几处需要更改。
第一个:
通常根页面 "/"
被映射到一个 index
命名函数。
@app.route('/', methods = ["GET","POST"])
def index():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
foo = request.form.get('name')
if not os.path.exists("/pictures/directory"):
os.makedirs("/pictures"+foo)
app.config["UPLOAD_FOLDER"] = "/pictures" + foo
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
return render_template("upload.html")
return render_template("index.html")
第二:
使用单个按钮(在本例中为 Update
)更新内容和重定向更有意义,然后您可以丢弃 Continue
按钮。
第三名:
在您的 upload.html 文件中,您必须更正表单代码
<form action="" method=post enctype=multipart/form-data>
到
<form action="{{ url_for("index") }}" method= "post" enctype= "multipart/form-data">
因此,您将处理此表单的函数的 url 作为值赋予 action
属性。最后,在值周围添加双引号。