简体   繁体   English

Flask 图像字段,如何在更新 wtform 中填充图像字段?

[英]Flask Image Field, How to populate image field in update wtform?

I have an app where user can add, update and delete virtual assistants.我有一个应用程序,用户可以在其中添加、更新和删除虚拟助手。 To update them I have used populate_obj method.为了更新它们,我使用了 populate_obj 方法。 It takes all fields from original virtual assistant like name, last_name and populates fields in update form, but it does not populate image field.它从原始虚拟助手中获取所有字段,如姓名、姓氏和填充更新表单中的字段,但不填充图像字段。 Because of that user has to upload photo even if he does not want to update this row.因为那个用户即使不想更新这一行也必须上传照片。 For example: user wants to update only name, he pushes the button update, writes new name, and pushes the button submitt, then he get information "choose photo", but he does not need to write new last_name it is populated and I want the same with photo.例如:用户只想更新姓名,他按下按钮更新,写新名字,然后按下按钮提交,然后他得到信息“选择照片”,但他不需要写新的姓氏,我想要照片也是一样。 I am wondering what can be solution here?我想知道这里有什么解决方案? I mean how can I populate photo row so user does not have to choose photo each time?我的意思是如何填充照片行以便用户不必每次都选择照片?

Model模型

class VirtualAssistant(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    last_name = db.Column(db.String(30))
    image_filename = db.Column(db.String)
    image_url = db.Column(db.String)

View:看法:

@app.route("/update/<int:id>/", methods=["GET", "POST"])
def update_virtual_assistant(id):
    updated_virtual_assistant = VirtualAssistant.query.get_or_404(id)
    form = UpdateVirtualAssistant(obj=updated_virtual_assistant)
    if request.method == "POST":
        if form.validate_on_submit():
            form.populate_obj(updated_virtual_assistant)
            if "photo" in request.files:
                photo = request.files["photo"]
                filename = photos.save(form.photo.data)
                url = photos.url(filename)
                path = "C:\\Users\\Luiza\\VirtualAssistants\\static\\img\\" + filename
                resize_photo(path)
                updated_virtual_assistant.image_url = url
                updated_virtual_assistant.image_filename = filename
            db.session.commit()
            return "the virtual assistant has been updated"
    return render_template("UpdateVirtualAssistant.html", updated_virtual_assistant=updated_virtual_assistant, form=form)

forms:形式:

class AddVirtualAssistant(FlaskForm):
    name = StringField("name", validators=[DataRequired()])
    last_name = StringField("last_name", validators=[DataRequired()])
    photo = FileField("photo", validators=[DataRequired()])

class UpdateVirtualAssistant(AddVirtualAssistant):
    id = IntegerField("id", validators=[DataRequired()])

HTML: HTML:

<form method="POST" enctype="multipart/form-data">
    {{ form.csrf_token }}
    {{ form.id }}</br>
    {{ form.name }}</br></br>
    {{ form.last_name }}</br></br>
    {{ form.photo }}</br></br>
    <button type="submit">Update assistant</button>
</form>

You should make a separate class for UpdateVirtualAssistant() instead.您应该为 UpdateVirtualAssistant() 创建一个单独的类。 This way the photo does not have the DataRequired() validator and you wouldn't have to worry about the "choose photo" validator flag in the first place.这样照片就没有 DataRequired() 验证器,您首先不必担心“选择照片”验证器标志。 Also consider using the FileAllowed() validator to only allow certain file types.还可以考虑使用 FileAllowed() 验证器来仅允许某些文件类型。

class UpdateVirtualAssistant(FlaskForm):
    id = IntegerField("id", validators=[DataRequired()])
    name = StringField("name", validators=[DataRequired()])
    last_name = StringField("last_name", validators=[DataRequired()])
    photo = FileField("photo", validators=[FileAllowed(['jpg','jpeg','png'])])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM