Flask(11)-博客文章
来源:互联网 发布:form表单提交数据加密 编辑:程序博客网 时间:2024/05/16 22:24
提交显示博客文章
- 创建文章模型Post
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key = True) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) author_id = db.Column(db.Integer, db.ForeignKey('users.id')class User(Usermixin, db.Model): posts = db.relationship('Post', backref='author', lazy='dynamic')
- 文章博客表单PostForm
- 处理文章博客的首页路由route(‘/’)
@main.route('/', methods=['GET', 'POST'])def index(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and form.validate_on_submit(): post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) return redirect(url_for('.index')) posts = Post.query.order_by(Post.timestamp.desc()).all() #这是一个列表 return render_template('index.html', form=form, posts=posts) #关键在于index模板中如何排列posts
?? 数据库需要真正的用户对象,因此要调用 _get_current_object() 方法。
- 显示博客文章的首页模板
{{ quick_form(form) }}{% for post in posts %} <a href="{{ url_for('.user', username=post.author.username) }}"> <img src="{{ post.author.gravatar(size=40) }}"># 将图像作为链接 </a> {{ moment(post.timestamp).fromNow() }} <a href="{{ url_for('.user', username=post.author.username) }}"> {{ post.author.username }}# 将名字作为链接 </a> {{ post.body }}{% endfor %}
在资料页显示博客
- 路由route(‘/user/<username>’)
- 修改user.html模板(将显示文章的代码独立出来为_posts.html模板)
{% include '_posts.html' %}...
分页显示博客文章
- 创建虚拟文章
- 对首页路由修改
def index(): page = request.args.get('page', 1, type=int) pagination = Post.query.order_by(Post.timestamp.desc()).paginate(page, per_page=10, error_out=False) posts = pagination.items return render_template('index.html', form=form, posts=posts, pagination=pagination)
0 0
- Flask(11)-博客文章
- Flask Web 开发 博客文章
- Flask 简单博客用户文章评论部分
- Flask Web 开发 博客文章_2
- Flask Web 开发 博客文章_3
- Flask Web 开发 博客文章_4
- Flask Web 开发 博客文章_5
- 《flask web开发》第11章 博客文章发布表单无法显示的解决方案
- flask博客中关于文章和图片位置的定义
- 用 Flask 来写个轻博客 (30) — 使用 Flask-Admin 增强文章管理功能
- 用flask开发个人博客(37)—— 使用Flask-pagedown实现博客文章预览的功能
- 用 Flask 来写个轻博客 (22) — 实现博客文章的添加和编辑页面
- 用flask开发个人博客(36)—— 使用SQLAlchemy对博客文章进行分页
- 利用Flask-SQLAlchemy提供的paginate()方法实现博客文章的分页显示
- flask轻博客
- Flask-实现博客基本功能
- 用flask开发个人博客(38)—— 使用MarkDown实现博客文章存储成富文本格式
- flask web开发创建虚拟博客文章数据bug AttributeError: 'NoneType' object has no attribute 'encode'的解决
- JavaWeb学习总结(三)——Tomcat服务器学习和使用(二)
- JAVA配置文件的读取
- 四张图包含了020所有的秘密
- 叔恶!今日凌晨潮汕一汽车掉进水沟内,车上三女生身亡!
- CSS3边框 , 背景
- Flask(11)-博客文章
- android L 上切换字库方法
- 在适配器的构造方法添加假数据,然后请求数据后listview等的图片显示不完整,刷新之后才显示的问题。
- SQLiteOpenHelper 类
- 小问题记录
- 【杭电-oj】-2030-汉字统计
- Java虚拟机参数设置
- 三角蛋糕
- 自动触发按钮的点击事件