我在Flask和Mongoengine中创建了一种博客,它可以跟踪某台机器生产的每一件作品。用户还可以对每个制作的部分添加评论。想象一下,它就像一个博客,里面有帖子(包含每个部分的信息),并且可以在每个帖子中添加评论.我想做的是,给用户添加评论的能力。多重 图片同时在其评论中。此外,我还想把图片存储在MongoDB中。对于单张图片这样做是没有问题的,但是对于更多的图片,我没有找到这样做的方法。
我最初的代码是:
模型.py
class Comment(Document):
#
meta = {'collection': 'comments'}
#
commentPartID = fields.StringField(required=True)
#
commentTitle = fields.StringField(required=True)
commentContent = fields.StringField(required=True)
commentInsertedAt = fields.DateTimeField(required=True, default=datetime.datetime.utcnow)
commentAuthor = fields.StringField(required=True)
#
commentImage = fields.ListField(fields.ImageField(size=(1200,800,False), thumbnail_size=(300,200,False), collection_name='images'))
commentCategory = fields.StringField()
commentTag = fields.StringField()
路由.py
def create_comment(machine_serial_number, part_number):
#
part = Part.objects(partNumber=part_number).first()
#
form = CommentForm(commentPartID=part.partID)
#
if request.method=='POST' and form.validate_on_submit():
#
comment = Comment(commentPartID=form.commentPartID.data,
commentTitle=form.commentTitle.data,
commentContent=form.commentContent.data,
commentAuthor=form.commentAuthor.data,
commentCategory=form.commentCategory.data)
comment.save()
#
#
#* Obtain the images from request
images = request.files.getlist('commentImage')
for image in images:
comment.update(add_to_set__commentImage=[image])
#
flash(f'Comment for part {part.partNumber} by {comment.commentAuthor} has been successfully saved.', 'success')
#
return redirect(url_for('main_blueprint.part', machine_serial_number=machine.machineSerialNumber, part_number=part.partNumber))
#
return render_template('create_comment.html', title='Create Comment', machine=machine, part=part, form=form)
表格.py
class CommentForm(FlaskForm):
commentPartID = HiddenField()
#
commentTitle = StringField('Title', validators=[DataRequired()])
commentContent = TextAreaField('Content', validators=[DataRequired()])
commentAuthor = StringField('Comment Author', validators=[DataRequired()])
#
commentImage = MultipleFileField('Image File', validators=[FileAllowed(['jpg', 'png'], 'Only image files may be uploaded!')])
commentCategory = SelectField('Category')
#
submit = SubmitField('Save Comment')
#
这给我带来了以下错误。
bson.errors.InvalidDocument: cannot encode object:<FileStorage: 'Screenshot (1).png' ('image/png')>, of type: <class 'werkzeug.datastructures.FileStorage'>
我目前的变通办法是:模型.py
class Comment(Document):
#
meta = {'collection': 'comments'}
#
commentPartID = fields.StringField(required=True)
#
commentTitle = fields.StringField(required=True)
commentContent = fields.StringField(required=True)
commentInsertedAt = fields.DateTimeField(required=True, default=datetime.datetime.utcnow)
commentAuthor = fields.StringField(required=True)
#
commentCategory = fields.StringField()
commentTag = fields.StringField()
#
#
class CommentImage(Document):
meta = {'collection': 'images'}
#
imageCommentID = fields.ObjectIdField()
imageContent = fields.ImageField(size=(1200,800,False), thumbnail_size=(300,200,False), collection_name='images')
#
路由.py
@comment_blueprint.route('/parts/<part_number>/add_comment', methods=['GET', 'POST'])
def create_comment(machine_serial_number, part_number):
#
part = Part.objects(partNumber=part_number).first()
#
form = CommentForm(commentPartID=part.partID)
#
if request.method=='POST' and form.validate_on_submit():
comment = Comment(commentPartID=form.commentPartID.data,
commentProjectName=form.commentProjectName.data,
commentTitle=form.commentTitle.data,
commentContent=form.commentContent.data,
commentAuthor=form.commentAuthor.data,
commentCategory=form.commentCategory.data).save()
#
#* Obtain comment ID; this is needed for the CommentImage to establish a link to the Comment
comment = Comment.objects().order_by('-commentInsertedAt').first()
#
#* Obtain the images from request
images = request.files.getlist('commentImage')
for image in images:
image = CommentImage(imageCommentID=comment.id, imageContent = image)
image.save()
#
flash(f'Comment for part {part.partNumber} by {comment.commentAuthor} has been successfully saved.', 'success')
#
return redirect(url_for('main_blueprint.part', machine_serial_number=machine.machineSerialNumber, part_number=part.partNumber))
#
return render_template('create_comment.html', title='Create Comment', machine=machine, part=part, form=form)
froms.py这一点一直没有改变。