Answer the question
In order to leave comments, you need to log in
How to fetch filefild from wtforms flask form?
hello there is a form
class Reg(Form):
foto = FileField('Фото')
send = SubmitField('Регистрация')
@applic.route('/', methods=['GET','POST'])
def main():
form = Reg(request.form)
if request.method == 'POST' and form.validate():
print(form.data)
print(form.errors)
print(form.validate())
return render_template('index.html', form = form)
form = Reg()
print(form.data)
<form action="" method="post" enctype="multipart/form-data">
<div class="col-lg-5 col-lg-offset-4">
{{ form.foto }}
{{form.csrf_token }}
{{ form.send( class ='form-control')}}
</div>
</form>
Answer the question
In order to leave comments, you need to log in
Not currently?
It's not clear, just the form for validation will be used or will it still work with the database through orm?
If I understand you correctly, then you don't need request.form at all. Make the form model in the form
class class Reg(ModelForm(IMPORTED_CLASS_FROM_DB_MODELS)):
override your fields here if you want.
on the view:
# get the database object
test123= reg()
# initialize the form
form = Reg(obj = test123
) shorter than if request.method == 'POST' and form.validate():
if form.validate_on_submit():
form.populate_obj(test123)
# and here we just commit
db.session.commit() right away
return render_template('index.html', form = form)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question