Skip to main content
 首页 » 编程设计

python之使用 FileField wtforms 上传文件时将文件名存储在列中

2024年12月31日8softidea

我有一个使用以下代码的表格,其中包括两列:名称和文件名。我的问题是,当我上传文件时,它的文件名存储在文件名列中。我该怎么做呢? 现在,当我上传文件时,“无”被放置在文件名中。我只能上传文件或在数据库中输入名称,我认为问题是 enctype="multipart/form-data"。 从 flask_wtf 导入 FlaskForm

from flask_wtf.file import FileField 
from wtforms import StringField,SelectField,IntegerField 
from wtforms.ext.sqlalchemy.fields import QuerySelectField 
from wtforms.validators import DataRequired, Email, Length 
class ContactForm(FlaskForm): 
 
    name = IntegerField('name', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')]) 
    filename = FileField() 

这是我的 app.py:

def new_contact(): 
    ''' 
    Create new contact 
    ''' 
    form = ContactForm() 
 
 
    if  form.validate_on_submit(): 
 
 
        return render_template('web/new_contact.html',form = form) 
 
        f = form.filename.data 
        f.save(os.path.join("./static/upload/", f.filename)) 
        return redirect(url_for('new_contact')) 
        print(f) 
 
 
 
        my_contact = Contact() 
        form.populate_obj(my_contact) 
        db.session.add(my_contact) 
        try: 
            db.session.commit() 
            # User info 
            flash('Contact created correctly', 'success') 
            return redirect(url_for('contacts')) 
        except: 
            db.session.rollback() 
            flash('Error generating contact.', 'danger') 
 
    return render_template('web/new_contact.html', form=form) 

请您参考如下方法:

根据您提供的有限信息,我会尝试实现您想要的功能。

您的 ContactForm 可以保持这样:

class ContactForm(FlaskForm): 
 
    name = IntegerField('File Name', validators=[Length(min=-1, max=100, message='You cannot have more than 100 characters')]) 
    filename = FileField() 

然后你将表单对象传递给模板,从自定义的 flask 路由,让我们调用它来解释,联系路由:

@app.route('/contact') 
def contact(): 
    contact_form = ContactForm() 
    return render_template('contact.html' 
                            contact_form = contact_form) 

在你的模板中,我在这个例子中调用 contact.html,你呈现你的表单:

<form action="" method="post" enctype="multipart/form-data"> 
    {{ contact_form.csrf_token }} 
    {{ contact_form.name }} 
    {{ contact_form.filename}} 
    <input type="submit"/> 
</form> 

在这种形式中,我们希望使用 action="" 在同一路由上发布数据,即 联系路由。所以在这个例子中,我们还应该验证 Flask 应用程序的 contact() 方法中的数据。但是您可能想知道 enctype="multipart/form-data" 是什么?

搜索它是什么的第一个结果给了我们结果:

The enctype attribute specifies how the form-data should be encoded when submitting it to the server. Note: The enctype attribute can be used only if method="post".

对于multipart/form-data:

No characters are encoded. This value is required when you are using forms that have a file upload control.

最后,我们像这样更新 flask 应用联系方式:

@app.route('/contact') 
def contact(): 
    contact_form = ContactForm() 
    if form.validate_on_submit(): 
        f = contact_form.filename.data 
        name = contact_form.name.data 
        f.save(os.path.join("./static/contacts/", name)) 
        redirect(url_for('contact')) 
    return render_template('contact.html' 
                            contact_form = contact_form) 

我们已经成功地从表单中收集了数据,并将文件静态保存在联系人文件夹中,名称来自表单。或许我们还可以使用 werkzeug.utils 中的 secure_filename。