编辑文章
发表之后
显示不了
具体代码如下
models.py
class Post(db.Model):
__tablename__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), unique=True, index=True)
body = db.Column(db.Text)
tiemstamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
body_html = db.Column(db.Text)
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tage = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
'h1', 'h2', 'h3', 'p']
target.body_html = bleach.linkify(bleach.clean(markdown(value, output_format='html'),
tags=allowed_tage, strip=True))
db.event.listen(Post.body, 'set', Post.on_changed_body)
app/main/viwes.py
@main.route('/dome',methods=['GET','POST'])
def dome():
form =PostForm()
if form.validate_on_submit():
p=Post(title=form.title.data,body=form.body.data,
author=current_user._get_current_object())
db.session.add(p)
db.session.commit()
return render_template('dome.html',form=form)
dome.html
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block content %}
<div class="row">
<form method="POST">
{{ form.hidden_tag() }}
<div class="col-md-11 form-group">
{{ form.title(class="form-control",placeholder='title') }}
</div>
<div class="form-group col-md-1">
{{ form.submit(class="btn btn-primary") }}
</div>
<div class="col-md-6 form-group">
{{ form.body(only_input=True,class="form-control",rows=20,
placeholder='Post') }}
</div>
<div class="col-md-6 form-group">
{{ form.body(only_preview=True,class="form-control",rows=20) }}
</div>
</form>
</div>
{% endblock %}
{% block scripts %}
{{ super() }}
{{ pagedown.include_pagedown() }}
{% endblock %}
allowed_tage 没有 img 图片给过滤掉了吧