Flask WTForms,将TextAreaField与FileField组合

时间:2020-02-26 08:38:46

标签: python flask jinja2 flask-sqlalchemy flask-wtforms

我寻求帮助,我正在尝试将TextAreaField和FileField组合在一起,但是我不知道该怎么做。我可以发布文字,图片或视频的更简单的方式。

这是我的PostForm

         @posts.route('/post/new', methods=['GET', 'POST'])
         @login_required
         def new_post():
            form = PostForm()
            if form.validate_on_submit():
                post = Post(title=form.title.data, 
                                content=form.content.data, 
                                             author=current_user)
                db.session.add(post)
                db.session.commit()
                flash('Your new post has been created!', 'success')
                return redirect(url_for('main.updates'))
            return render_template('create_post.html', title='New Post', 
                                           form=form, legend="New Post")


           @posts.route('/post/<int:post_id>')
           def post(post_id):
           post = Post.query.get_or_404(post_id)
           return render_template('post.html', title=post.title, 
           post=post)

这是我的发布路线

        <form class="form-signin" method="POST" action="" 
        enctype="multipart/form-data">
        <legend class="border-bottom mb-4"> {{ legend }} </legend>
        {{ form.hidden_tag() }}
        <div class="form-group">
        {{ form.title.label(class="form-control-label") }}
        {% if form.title.errors %}
        {{ form.title(class="form-control form-control-lg is-invalid") }}
        <div class="invalid-feedback">
            {% for error in form.title.errors %}
            <span>{{ error }}</span>
            {% endfor %}
        </div>
        {% else %}
        {{ form.title(class="form-control form-control-lg") }}
        {% endif %}
        </div>

        <div class="form-group">
        {{ form.content.label(class="form-control-label") }}
        {% if form.content.errors %}
        {{ form.content(class="form-control form-control-lg is-invalid") 
        }}
        <div class="invalid-feedback">
        {% for error in form.content.errors %}
        <span>{{ error }}</span>
        {% endfor %}
        </div>
        {% else %}
        {{ form.content(class="form-control form-control-lg", id='editor') 
        }}
        {% endif %}
        </div>

        <div class="form-group">
            {{ form.submit(class="btn btn-outline-info") }}
        </div>
        </form>

这是我的帖子模板

{{1}}

请,您能告诉我如何将TextAreaField和FileField结合在一起,以便我可以一种形式发布文本或图像或视频。预先感谢

1 个答案:

答案 0 :(得分:0)

以该示例为例,并相应地编写逻辑。 :

烧瓶形式:

class UploadForm(FlaskForm):
    name = StringField('Name', validators=[validators.required()])
    image = FileField('Image', validators=[validators.required()])
    submit = SubmitField('Submit')

routes.py:

@app.route("/upload", methods=['GET', 'POST'])
def upload():
    form = UploadForm()
    if request.form.get('submit') == 'Submit':
        filename1 = secure_filename(form.image.data.filename)
        form.image.data.save(app.config['UPLOAD_FOLDER'] + filename1)
        url = images.url(filename1)
        post = Post(name=form.name.data, image_filename=filename1, image_url=url, author=current_user)
        db.session.add(post)
        db.session.commit()
        ...

Model.py:

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(140))
    image_filename = db.Column(db.String, default=None, nullable=True)
    image_url = db.Column(db.String, default=None, nullable=True)

以及一些配置:

# Configure the image uploading via Flask-Uploads
images = UploadSet('images', IMAGES)
configure_uploads(app, images)