我正在构建Flask应用程序,我希望能够发布图像/视频。当我运行以下代码时,它似乎已加载,但是刷新页面时,我看不到我上传的图像文件。任何帮助将不胜感激。这是相关的代码。我假设我可能必须在models.py中的Post类中添加一列?
这是Forms.py
class PostForm(Form):
body = PageDownField("What's on your mind DS?",
validators = [Required()])
post_type = HiddenField("post_type")
image_file = FileField('Image file')
submit = SubmitField('Submit')
这是我的观点。py
@server.route('/post_video/<int:id>', methods=['GET', 'POST'])
def upload_file():
image = None
form = PostForm()
if form.validate_on_submit():
image = 'uploads/' + form.image_file.data.filename
form.image_file.data.save(os.path.join(ds.static, image))
return render_template('post.html', form=form, image=image)
这是Models.py
class Post(db.Model):
__tablename__ = 'posts'
__table_args__ = {'extend_existing': True}
id=db.Column(db.Integer, primary_key = True)
body = db.Column(db.Text)
body_html = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True,
default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
post_type = db.Column(db.String(10))
comments = db.relationship('Comment', backref='post', cascade='all,
delete-orphan', lazy='dynamic')
这是post.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}DS - Post{% endblock %}
{% block content %}
{% include '_posts.html' %}
<h4 id="comments">Leave A Comment</h4>
<div class="comment-form">
{{ wtf.quick_form(form) }}
</div>
{% include '_comments.html' %}
{% endblock %}