예제 #1
0
def edit(id=0):
    form = PostForm()
    if id == 0:
        # 新增
        post = Post(users=current_user)
    else:
        # 修改
        post = Post.query.get_or_404(id)
        form.title.data = post.title
        form.body.data = post.body

    if form.validate_on_submit():
        form = PostForm()
        post.title = form.title.data
        post.body = form.body.data

        # post = Post(title=form.title.data, body=form.body.data)

        db.session.add(post)
        db.session.commit()
        return redirect(url_for('main.post', id=post.id))

    title = '添加新文章'
    if id > 0:
        title = '编辑 - %s' % post.title
    return render_template('edit.html', title=title, form=form, post=post)
예제 #2
0
def new_post():
    form = PostForm()
    if request.method == 'POST' and form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.categories.data)
        post = Post()
        post.title = title
        post.category = category
        post.body = body
        try:
            db.session.add(post)
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            db.session.rollback()
            raise e
        flash('新建了一篇文章', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)