问题描述
我在使文件上传在 cookiecutter-flask 应用程序(v.0.10.1)中工作时遇到问题).目前,它不会保存上传的文件.
I am having a problem getting a file upload to work in a cookiecutter-flask app (v. 0.10.1). Right now, it is not saving the file uploaded.
Cookiecutter-Flask默认安装WTForms和Flask-WTForms.我尝试将Flask-Uploads添加到此,但是我不相信该模块在此时添加了任何内容,因此我将其卸载了.这是Flask-WTF文件上传文档: http://flask-wtf.阅读thedocs.io/en/latest/form.html#module-flask_wtf.file
Cookiecutter-Flask by default installs WTForms and Flask-WTForms. I have tried adding Flask-Uploads to this but I'm not convinced that module adds anything at this point so I have uninstalled it. This is the Flask-WTF file upload documentation: http://flask-wtf.readthedocs.io/en/latest/form.html#module-flask_wtf.file
文档与我的应用程序之间的主要区别在于,我似乎在更多文件中都具有信息,并且符合cookiecutter的约定.
The main difference between the documentation and my app is that I seem to have information across more files, in keeping with the conventions of the cookiecutter.
在app_name/spreadsheet/forms.py
中:
from flask_wtf import Form
from wtforms.validators import DataRequired
from flask_wtf.file import FileField, FileAllowed, FileRequired
class UploadForm(Form):
"""Upload form."""
csv = FileField('Your CSV', validators=[FileRequired(),FileAllowed(['csv', 'CSVs only!'])])
def __init__(self, *args, **kwargs):
"""Create instance."""
super(UploadForm, self).__init__(*args, **kwargs)
self.user = None
def validate(self):
"""Validate the form."""
initial_validation = super(UploadForm, self).validate()
if not initial_validation:
return False
在app_name/spreadsheet/views.py
中:
from flask import Blueprint, render_template
from flask_login import login_required
from werkzeug.utils import secure_filename
from app_name.spreadsheet.forms import UploadForm
from app_name.spreadsheet.models import Spreadsheet
from app_name.utils import flash, flash_errors
blueprint = Blueprint('spreadsheet', __name__, url_prefix='/spreadsheets', static_folder='../static')
@blueprint.route('/upload', methods=['GET', 'POST']) #TODO test without GET since it won't work anyway
@login_required
def upload():
uploadform = UploadForm()
if uploadform.validate_on_submit():
filename = secure_filename(form.csv.data.filename)
uploadform.csv.data.save('uploads/csvs/' + filename)
flash("CSV saved.")
return redirect(url_for('list'))
else:
filename = None
return render_template('spreadsheets/upload.html', uploadform=uploadform)
这是命令行输出,显示我上传文件时没有错误:
This is the command line output showing no errors when I upload a file:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Sep/2016 10:29:10] "GET /spreadsheets/upload HTTP/1.1" 200 -
127.0.0.1 - - [04/Sep/2016 10:29:10] "GET /_debug_toolbar/static/css/toolbar.css?0.3058158586562558 HTTP/1.1" 200 -
127.0.0.1 - - [04/Sep/2016 10:29:14] "POST /spreadsheets/upload HTTP/1.1" 200 -
127.0.0.1 - - [04/Sep/2016 10:29:14] "GET /_debug_toolbar/static/css/toolbar.css?0.3790246965220061 HTTP/1.1" 200 -
对于uploads/csvs
目录,我尝试了绝对路径和相对路径,并且该目录的权限为766.
For the uploads/csvs
directory I have tried absolute and relative paths and the directory is permissioned 766.
模板文件为:
{% extends "layout.html" %}
{% block content %}
<h1>Welcome {{ session.username }}</h1>
{% with uploadform=uploadform %}
{% if current_user and current_user.is_authenticated and uploadform %}
<form id="uploadForm" method="POST" class="" action="{{ url_for('spreadsheet.upload') }}" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<div class="form-group">
{{ uploadform.csv(class_="form-control") }}
</div>
<button type="submit" class="btn btn-default">Upload</button>
</form>
{% endif %}
{% endwith %}
{% endblock %}
哪个生成以下HTML:
Which generates this HTML:
<form id="uploadForm" method="POST" class="" action="/spreadsheets/upload" enctype="multipart/form-data">
<input type="hidden" name="csrf_token" value="LONG_RANDOM_VALUE"/>
<div class="form-group">
<input class="form-control" id="csv" name="csv" type="file">
</div>
<button type="submit" class="btn btn-default">Upload</button>
</form>
推荐答案
我认为有一种更简单的上传文件的方法.这是我实施的,希望对您有所帮助.因为您当前的要求看起来与我的要求相似,所以您的解决方案看起来有些复杂.
There is a simpler way to upload files in my opinion.This is something I implemented, hope it can be of help to you. Cause your current requirement looks similar to mine yet, your solution looks a little complex.
所以我想制作一个pdf上传器页面,这就是我所做的.
So I wanted to make a pdf uploader page, this is what I did.
- 转到config.py文件或定义sql数据库链接的位置
UPLOAD_FOLDER = r'C:\location\app\upload'
ALLOWED_EXTENSIONS = {'pdf'}
- 转到您的视图或路线并编写,以检查上载的文件是否符合扩展名要求.
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
- 然后,我在这里所做的是我制作了一种在数据库表中存储文件名的方法.当我调用一个函数时,它会在文件夹中查找该特定文件名,然后检索并显示给我.
- Then,what i did here is i made a method to store a filename in a table in database. When i call a function,it looks in the folder for that particular filename and retrieves and shows it to me.
@app.route("/#route details here", methods=['GET', 'POST'])
def xyz():
if request.method == 'POST':
if 'file' not in request.files:
flash(f'No file part', 'danger')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash(f'No selected file', 'danger')
return redirect(request.url)
if file and allowed_file(file.filename): #allowed file is the definition i created in point 2.
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #save file in a target folder.
new_report = Report(report_name=filename, report_welder_wps_association_id=report_id) #create a database entry with exact filename
db.session.add(new_report)
db.session.commit()
return redirect(url_for(#redirection on success condition))
return render_template(#render template requirements go here)
- 最后是一个在我请求时获取文件的视图.我只是查询数据库,获取文件名,然后使用文件名作为参数将其重定向到该视图,然后从目标文件夹中吐出文件.
- And finally a view to obtain the file whenever i request it.I just query my database, get the filename and redirect it to this view with the filename as parameter, and it spits out the file from the target folder.
@app.route('/upload/<filename>')
def uploaded_file(filename) -> object:
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
这是我需要定义的唯一形式:
And this is the only form i need to define :
class XYZ(db.Model):
__tablename__ = 'xyz'
uploaded_file_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
uploaded_file_name = db.Column(db.String(300), nullable=False)
这篇关于Cookiecutter-flask应用中的文件未使用Flask-wtforms上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!