评论在数据库中的表示

由于评论和2个模型有关系,分别是谁发了评论,以及评论了哪个文章,所以这次要更新数据库模型

models.py 创建用户评论数据库模型

class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.Text)
timestamp = db.Column(db.DateTime,index=True,default=datetime.utcnow)
body_html = db.Column(db.Text)
disabled = db.Column(db.Boolean) # 管理员用来查禁不当评论
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
post_id = db.Column(db.Integer,db.ForeignKey('posts.id')) @staticmethod
def on_changed_body(target,value,oldvalue,initiator):
allowed_tags=['a','abbr','acronym','b','code','em','i','strong']
target.body_html=bleach.linkify(bleach.clean(markdown(value,output_format='html'),tags=allowed_tags,strip=True))
db.event.listen(Comment.body,'set',Comment.on_changed_body)

User和Post里面也要和Comment设置相应的关系

class User(db.Model):
# ...
comments = db.relationship('Comment', backref='author', lazy='dynamic')
class Post(db.Model):
# ...
comments = db.relationship('Comment', backref='post', lazy='dynamic')

提交和显示评论

main/forms.py 创建评论输入表单

class CommentForm(FlaskForm):
body = StringField('输入你的评论',validators=[DataRequired()])
submit = SubmitField('提交')

为支持评论更新路由post

main/views.py 支持博客文章评论

# 文章固定链接
@main.route('/post/<int:id>', methods=['GET', 'POST'])
def post(id):
post = Post.query.get_or_404(id)
form = CommentForm()
if form.validate_on_submit():
# current_user(上下文代理对象)._get_current_object() 真正的User对象
comment = Comment(body=form.body.data,
post=post,
author=current_user._get_current_object())
db.session.add(comment)
flash('你已经发布了新评论.')
# page设为-1,用来请求评论的最后一页,刚提交的评论才会出现在页面中
return redirect(url_for('.post', id=post.id, page=-1))
page = request.args.get('page', 1, type=int)
# 程序获取页数,发现时-1时,会计算总量和总页数得出真正显示的页数
if page == -1:
page = (post.comments.count() - 1) // \
current_app.config['FLASKY_COMMENTS_PER_PAGE'] + 1
pagination = post.comments.order_by(Comment.timestamp.asc()).paginate(
page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
error_out=False)
comments = pagination.items
return render_template('post.html', posts=[post], form=form,
comments=comments, pagination=pagination)

评论模板 _comment.html

<ul class="comments">
{% for comment in comments %}
<li class="comment">
<div class="comment-thumbnail">
<a href="{{ url_for('.user', username=comment.author.username) }}">
<img class="img-rounded profile-thumbnail" src="{{ comment.author.gravatar(size=40) }}">
</a>
</div>
<div class="comment-content">
<div class="comment-date">{{ moment(comment.timestamp).fromNow() }}</div>
<div class="comment-author"><a href="{{ url_for('.user', username=comment.author.username) }}">{{ comment.author.username }}</a></div>
<div class="comment-body">
{% if comment.body_html %}
{{ comment.body_html | safe }}
{% else %}
{{ comment.body }}
{% endif %}
</div>
</div>
</li>
{% endfor %}
</ul>

评论引入post.html

{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import '_macros.html' as macros %}
{% block title %}Flasky - 文章{% endblock %} {% block page_content %}
{% include '_posts.html' %} <h4 id="comments">评论</h4>
{% if current_user.can(Permission.COMMENT) %}
<div class="comment-form">
{{ wtf.quick_form(form) }}
</div>
{% endif %}
{% include '_comments.html' %}
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.post', fragment='#comments', id=posts[0].id) }}
</div>
{% endif %}
{% endblock %}

上面加在 _posts.html里面的这段话,在url地址后面,加了一个 #comments

这个叫做URL片段

可以理解为:#这个符号,在URL地址里面相当于分隔符

#左边的,代表真实URL,对外的,就是普通用户可以看到的URL

#右边的,代表对网页内部的代码,在浏览器的地址栏是看不到的,他针对的是,当前页面HTML文件内,id=comments的内容

具体作用是:当加载这个页面时,直接会将这个id处于的位置,滚到页面顶端

类似于平时的用户体验是:当评论完以后,页面重新加载,评论区域会自动翻滚到页面最上端,体验很好。

按了comments按钮以后,可以直接跳转到post页面的评论区域,等于是个页中页功能。

链接到博客的评论_post.html

<a href="{{ url_for('.post', id=post.id) }}#comments">
<span class="label label-primary">{{ post.comments.count() }} 评论</span>
</a>

user.html 增加评论数

<p><b> 评论数量:</b>{{ user.comments.count() }}.</p>

_macro.html 宏需要加入片断参数

{% macro pagination_widget(pagination,endpoint,fragment='') %}
<ul class="pagination">
<li {% if not pagination.has_prev %}class="disabled" {% endif %}>
<a href="{% if pagination.has_prev %}{{ url_for(endpoint,page = pagination.prev_num,**kwargs) }}{{ fragment }}{% else %}#{% endif %}">
&laquo;
</a>
</li>
{% for p in pagination.iter_pages() %}
{% if p %}
{% if p ==pagination.page %}
<li class="active">
<a href="{{ url_for(endpoint,page=p,**kwargs) }}{{ fragment }}">{{ p }}</a>
</li>
{% else %}
<li>
<a href="{{ url_for(endpoint,page=p,**kwargs) }}{{ fragment }}">{{ p }}</a>
</li>
{% endif %}
{% else %}
<li class="disabled"><a href="#">&hellip;</a></li>
{% endif %}
{% endfor %}
<li {% if not pagination.has_next %}class="disabled" {% endif %}>
<a href="{% if pagination.has_next %}{{ url_for(endpoint,page = pagination.next_num,**kwargs) }}{{ fragment }}{% else %}#{% endif %}">
&raquo;
</a>
</li>
</ul>
{% endmacro %}

管理评论

base.html加入评论管理导航条

{% if current_user.can(Permission.MODERATE_COMMENTS) %}
<li>
<a href="{{ url_for('main.moderate') }}">评论管理</a>
</li>
{% endif %}

main/views.py 管理评论路由

@main.route('/moderate')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate():
page = request.args.get('page', 1, type=int)
pagination = Comment.query.order_by(Comment.timestamp.desc()).paginate(
page, per_page=current_app.config['FLASKY_COMMENTS_PER_PAGE'],
error_out=False)
comments = pagination.items
return render_template('moderate.html',comments=comments,pagination=pagination,page=page)

moderate.html 评论管理页面模板

{% extends "base.html" %}
{% import '_macros.html' as macros %}
{% block title %}Flasky - 评论管理{% endblock %} {% block page_content %}
<div class="page-header">
<h1>评论管理</h1>
</div>
{% set moderate = True %} # 决定是否渲染评论管理功能
{% include '_comments.html' %}
{% if pagination %}
<div class="pagination">
{% if pagination %}
<div class="pagination">
{{ macros.pagination_widget(pagination, '.moderate') }}
</div>
{% endif %}
</div>
{% endif %}
{% endblock %}

_comments.html 评论正文

 <div class="comment-body">
{% if comment.disabled %}
<p><i>此评论已被管理员禁用</i></p>
{% endif %}
{% if moderate or not comment.disabled %}
{% if comment.body_html %}
{{ comment.body_html | safe }}
{% else %}
{{ comment.body }}
{% endif %}
{% endif %}
</div>
{% if moderate %}
         <br>
{% if comment.disabled %}
<a class="btn btn-default btn-xs" href="{{ url_for('.moderate_enable',id=comment.id,page=page) }}">恢复</a>
{% else %}
<a class="btn btn-default btn-xs" href="{{ url_for('.moderate_disable',id=comment.id,page=page) }}">禁用</a>
{% endif %}
{% endif %}
</div>

main.views.py 评论管理路由

@main.route('/moderate/enable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_enable(id):
comment = Comment.query.get_or_404(id)
comment.disabled = False
db.session.add(comment)
return redirect(url_for('.moderate',page=request.args.get('page',1,type=int))) @main.route('/moderate/disable/<int:id>')
@login_required
@permission_required(Permission.MODERATE_COMMENTS)
def moderate_disable(id):
comment = Comment.query.get_or_404(id)
comment.disabled = True
db.session.add(comment)
return redirect(url_for('.moderate',page=request.args.get('page',1,type=int)))

Flask 学习 十二 用户评论的更多相关文章

  1. flask学习(十二):for循环遍历

    一. 字典的遍历 语法和python一样,可以使用items().keys().values().iteritems().iterkeys().itervalues() {% for k, v in ...

  2. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

  3. Flask 学习篇二:学习Flask过程中的记录

    Flask学习笔记: GitHub上面的Flask实践项目 https://github.com/SilentCC/FlaskWeb 1.Application and Request Context ...

  4. Scala学习十二——高阶函数

    一.本章要点 在Scala中函数是”头等公民“(可以作为参数,返回值,赋值给其他); 可以创建匿名函数,通常还会交给其他函数; 函数参数可以给出需要稍后执行的行为; 许多集合方法都接受函数参数,将函数 ...

  5. Flask学习之二 模板

    继续学习flask 本部分Miguel Grinberg教程的翻译地址:http://www.pythondoc.com/flask-mega-tutorial/templates.html 英文原文 ...

  6. Flask 学习(二)路由

    Flask  路由 在说明什么是 Flask 路由之前,详细阐述下 Flask “Hello World” 这一 最小应用的代码. Flask “Hello World” from flask imp ...

  7. java web 学习十二(session)

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  8. Flask 学习 十四 测试

    获取代码覆盖报告 安装代码覆盖工具 pip install coverage manage.py 覆盖检测 COV = None if os.environ.get('FLASK_COVERAGE') ...

  9. Flask 学习 十 博客文章

    提交和显示博客文章 app/models.py 文章模型 class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer ...

随机推荐

  1. javascript学习记录-2-18

    对象定义的几种方法: var  person=new Object(); person.name="111"; person.age=22; 或 var person={   na ...

  2. 【Luogu1291】百事世界杯之旅(动态规划,数学期望)

    [Luogu1291]百事世界杯之旅(动态规划,数学期望) 题面 洛谷 题解 设\(f[i]\)表示已经集齐了\(i\)个名字的期望 现在有两种方法: 先说我自己的: \[f[i]=f[i-1]+1+ ...

  3. 【POJ 3401】Asteroids

    题面 Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x ...

  4. 【Spring源码分析】Bean加载流程概览

    代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. 很多朋友可能想看Spring源码,但是不知道应当如何入手去看,这个可以理解:Java开发者通常从事 ...

  5. linux 记录用户操作日志

    将以下加入到/etc/profile 最后 history USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]/ ...

  6. Problem : 1002 ( A + B Problem II )

    经验总结:一定要注意输出的格式,字符的空格,空行,一定要观察清楚.如本题的最后一个输出结果后面没有空行.最后代码实现的时候需要判断一下,代码如下 !=n) cout<<endl; Prob ...

  7. Flyway--数据库版本管理和控制工具

    1. Flyway 的主要任务是管理数据库的版本更新,在Flyway 中称每次数据库更新为一个migration ,为了更顺口,我们下面称之为数据库脚本.Flyway 支持SQL-based migr ...

  8. java swing 下拉框与文本框

    import java.awt.*; import javax.swing.*; import javax.swing.border.*; import java.awt.event.*; publi ...

  9. 笔记:Struts2 Action 非泛型集合元素类型转换

    局部类型转换文件 局部类型转换文件的文件名应为 ActionName-conversion.properties,其中 ActionName 是需要替换为 Action 的类名称,后面的 conver ...

  10. 【数据结构】 字符串&KMP子串匹配算法

    字符串 作为人机交互的途径,程序或多或少地肯定要需要处理文字信息.如何在计算机中抽象人类语言的信息就成为一个问题.字符串便是这个问题的答案.虽然从形式上来说,字符串可以算是线性表的一种,其数据储存区存 ...