wtform无法使用包含多个字段的表单上载文件

时间:2019-02-04 04:58:07

标签: flask mongoengine wtforms

首先我的模型是这样的

    file = db.StringField(db_field = "f", required=True, unique=True)
    file_tn = db.StringField(db_field = "tn")
    descp = db.StringField(db_field="d", max_length=200)
    clr = db.IntField(db_field="cl", choices = COLOR, required=True)    
    prize = db.StringField(db_field="p")
    size = db.IntField(db_field="s", required=True)
    create_date = db.LongField(db_field="c", default=nowms()())
    from_user = db.ReferenceField(User, db_field="fu")

您可能猜对了!

我尝试创建带有大小,颜色等详细信息的上传表单,并将其全部存储在数据库中

但是问题是当我创建这样的表单

class UpLoadForm(Form):
    files = FileField('File', validators=[FileAllowed(['jpg','jpeg','png','gif','pdf']),validators.DataRequired()])

    size = SelectField('Paper\'s size',[validators.DataRequired()], coerce=int)

    color = SelectField('BW or Color', [validators.DataRequired()], choices=[(1,"Color"),
                                                                           (0,"Mono")])

是一个具有动态选择的SelectField


@core.route('/file/upload', methods=('GET','POST'))
def upload():
    ref = request.referrer
    form = UpLoadForm()

    #get store objects which I stored list of tuples for use in dynamic choices
    #it's look like this [(3,'A3'),(4,'A4')] yeah I'm creating a printing app
    store = Store.objects.filter(storecode='123456').first()    

    #store.pages contained list of tuples
    form.size.choices = store.pages

    #this will get logged in user objects to know who upload it
    logged_user = User.objects.filter(email = session.get('email')).first()


    if form.validate_on_submit():
        assert request.files.get('files') is not None
        if request.files.get('files'):            
            filename = secure_filename(form.files.data.filename)
            filepath = os.path.join(UPLOAD_FOLDER_FILES, filename)
            form.files.data.save(filepath)

            #it won't upload even when I hard code clr and size input(size = 1, clr = 1)
            file = File(
                file = filename,
                clr = form.color.data,
                size = form.size.data,
                from_user = logged_user
            )

            file.save()


我不知道为什么它不起作用

1.file甚至没有上传

2。没有断言错误

3。什么都没有保存到数据库

我尝试了什么

1。创建另一个将FileField分开的表单

结果? 它以某种方式工作

class UpLoadForm(Form):
    files = FileField('File', validators=[FileAllowed(['jpg','jpeg','png','gif','pdf']),validators.DataRequired()])


class FileDetail(Form):
    size = SelectField('Paper\'s size',[validators.DataRequired()], coerce=int)
    color = SelectField('Bw or Color', [validators.DataRequired()], choices=[(1,"Color"),
                                                                           (0,"Mono")])

除了我刚刚添加了第二种形式外,其他一切都差不多

@core.route('/file/upload', methods=('GET','POST'))
def upload():
    ref = request.referrer
    form = UpLoadForm()

#here is the second form I added
    store = Store.objects.filter(storecode='123456').first()
    form2 = FileDetail()
    form2.size.choices = store.pages

    logged_user = User.objects.filter(email = session.get('email')).first()
    if form.validate_on_submit():
        if request.files.get('files'):
           ###just the same###
            filename = secure_filename(form.files.data.filename)
            filepath = os.path.join(UPLOAD_FOLDER_FILES, filename)
            form.files.data.save(filepath)
           ###################

            file = File(
                file = filename,
                clr = form2.color.data,
                size = form2.size.data,
                from_user = logged_user
            )

            file.save()
        file = None
        if ref:
            return redirect(ref)
        else:
            return redirect(url_for('core.allstore'))
    else:
        return "There is no spoon"

HTML的一面是正确的,如果您认为它与问题有关(问题已经很久了),我可以告诉您

它现在可以工作,但我不知道为什么它首先不起作用

对不起,英语很抱歉 随时纠正所有问题或要求更多信息

0 个答案:

没有答案