Exemple #1
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        post = Article(title=title, body=body)
        # same with:
        # category_id = form.category.data
        # post = Post(title=title, body=body, category_id=category_id)
        db.session.add(post)
        db.session.commit()
        flash('Post created.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #2
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category = Category.query.get(form.category.data)
        db.session.commit()
        flash('Post updated.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    form.title.data = post.title
    form.body.data = post.body
    form.category.data = post.category_id
    return render_template('admin/edit_post.html', form=form)
Exemple #3
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        logger.debug(form.data)
        # 创建新文章
        post = Post(
            title=form.title.data,
            body=form.body.data,
            category_id=form.category.data,
        )
        db.session.add(post)
        db.session.commit()
        flash('文章发表成功!', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #4
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category)
        # 等价于:
        # category_id = form.category.data
        # post = Post(title=title, body=body, category_id=category_id)
        db.session.add(post)
        db.session.commit()
        flash('新建成功。', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #5
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category = Category.query.get(
            form.category.data)  #特别注意!,表单的分类字段是分类记录的id值
        db.session.commit()
        flash('Edit post success!', 'success')
        return redirect(url_for('blog.show_post', post_id=post_id))
    form.title.data = post.title
    form.category.data = post.category_id  #特别注意!
    form.body.data = post.body
    return render_template('admin/edit_post.html', form=form)
Exemple #6
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title,
                    body=body,
                    slug=slugify(title, max_len=240),
                    category=category)

        db.session.add(post)
        db.session.commit()
        flash('Post created.', 'success')
        return redirect(url_for('blog.show_post', slug=post.slug))
    return render_template('admin/new_post.html', form=form)
Exemple #7
0
def new_post():
    """创建新文章界面路由函数

    Returns:
        创建新文章界面
    """
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category)
        db.session.add(post)
        db.session.commit()
        flash('Post Created', 'Success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Exemple #8
0
def edit_post(post_id):
    '''编辑博客的条目'''
    post = Post.query.get_or_404(post_id)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.body = form.body.data
        post.category.name = form.category.default
        return redirect(url_for('blog.show_post'))

    form.title.data = post.title
    form.body.data = post.body
    form.category.default = post.category.name
    return render_template(
        'admin/edit_post.html',
        form=form,
    )
Exemple #9
0
def edit_post(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)
    if form.validate_on_submit():
        print(form.data)
        title = form.title.data
        category = Category.query.get(form.category.data)
        body = form.body.data
        post.title = title
        post.category = category
        post.body = body
        db.session.add(post)
        db.session.commit()
        flash('修改文章成功', 'success')
        return redirect(url_for('blog.show_post', post_id=post_id))
    form.title.data = post.title
    form.category.date = post.category.id
    form.body.data = post.body
    return render_template('admin/edit_post.html', form=form, post_id=post_id)