M
M
Michael2015-10-08 16:43:38
Flask
Michael, 2015-10-08 16:43:38

How to fetch filefild from wtforms flask form?

hello there is a form

class Reg(Form):
    foto = FileField('Фото')
    send = SubmitField('Регистрация')

eat controller
@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)

if we just do it
form = Reg()
, then the data from the form comes, but if we add the request.form argument when creating an instance, then it doesn’t. why?
if you write
print(form.data)
it without arguments, that is, like this, form = Reg() comes the file object, and with the request.form argument, then like this, form = Reg(request.form) comes with puffy quotes,
if you write print(form.validate()) it shows false

here is the form
<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>


How to make everything okay?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xzfallen, 2015-11-03
@nextel

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)

J
JRazor, 2015-10-09
@JRazor

And why do you put Request.form, actually? He's not needed here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question