我目前正在努力为我的 flaskblog 中的帖子添加图片
这是我的 routes.py 导入和 new_post 函数和路由
import os
import secrets
from PIL import Image
from flask import render_template, url_for, flash, redirect, request, abort
from flaskblog import app, db, bcrypt
from flaskblog.forms import RegistrationForm, LoginForm, UpdateAccountForm, PostForm
from flaskblog.models import User, Post
from flask_login import login_user, current_user, logout_user, login_required
@app.route("/post/new", methods=['GET', 'POST'])
@login_required
def new_post():
form = PostForm()
if form.validate_on_submit():
if form.post_picture.data:
image_post = form.post_picture.data
post = Post(title=form.title.data, content=form.content.data, author=current_user, post_image=image_post)
db.session.add(post)
db.session.commit()
flash('Your post has been created!', 'success')
return redirect(url_for('home'))
return render_template('create_post.html', title='New Post', post_image=post_image,
form=form, legend='New Post')
这是我的 forms.py 导入和 PostForm 类
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from flask_login import current_user
from wtforms import StringField, PasswordField, SubmitField, BooleanField, TextAreaField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from flaskblog.models import User
class PostForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
content = TextAreaField('Content', validators=[DataRequired()])
post_picture = FileField('Add image to your post', validators=[FileAllowed(['jpg', 'png'])])
submit = SubmitField('Post')
这是我的 models.py 文件和 Post 类
from datetime import datetime
from flaskblog import db, login_manager
from flask_login import UserMixin
江户川乱折腾
斯蒂芬大帝
明月笑刀无情
相关分类