I have a table using the following code, which includes two columns: name and filename. My problem is that when I upload the file , then its file name is stored in the filename column. How do I do this? Now when I upload the file, 'None' is placed in the filename.i can only upload file or enter name in database,i think that problem is enctype="multipart/form-data". from flask_wtf import 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()
this is my 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)