我是暑假期间的学生之家,正在创建应用程序,但我陷入了困境。我无法确定通过邮寄表格传递图片时要使用哪个字段。只是一个StringField引用我的操作系统上的图像,并在sqlite3数据库中使用相同的字符串吗?我会使用相同的引用通过jinja在html中显示它吗?一切正常,当我提交带有图像的帖子时没有调试器屏幕,但是图像未加载到数据库中。
@posts.route("/post/new", methods=['GET', 'POST'])
@login_required
def new_post():
if request.method == 'POST':
photo = request.files.get('photo')
content = request.form.get('content' if not (photo and content):
flash('You must fill in all the fields', 'caution')
elif:
try:
filename = uploaded_photos.save(photo)
except UploadNotAllowed:
flash('Upload type unpermitted', 'danger')
else:
post = Post(content=content, filename=filename)
db.session.add(post)
db.session.commit()
flash('Your post has been created!', 'success')
return redirect(url_for('main.home'))
return render_template('create_post.html', title='Create Post',form=form,
legend='Create Post')
class Post(SearchableMixin, db.Model):
__searchable__ = ['content']
id = db.Column(db.Integer, primary_key=True)
date_posted = db.Column(db.DateTime, nullable=False,
default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
# photo = ?
# video = ?
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
def __repr__(self):
return f"Post('{self.content}', '{self.date_posted}')"
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, TextAreaField
from wtforms.validators import DataRequired
class PostForm(FlaskForm):
content = TextAreaField('Content', validators=[DataRequired()])
submit = SubmitField('Post')
# photo = ?
# video = ?