Example #1
0
def add_post(id):
    edit = True if id != None else False
    if edit and Post.query.get(id) == None:
        abort(404)
    if edit:
        post = Post.query.get(id)
    else:
        post = None
    if request.method == 'POST':
        if not edit:
            post = Post(request.form['title'], request.form['content'], [])
        if not (request.form['title'] and request.form['content']):
            flash('Both title and content need to be specified.')
            return render_template('add_post.html', post=post, edit=edit)
        else:
            if edit:
                post.title = request.form['title']
                post.content = request.form['content']
                db_session.commit()
                flash('Post edited.')
            else:
                db_session.add(post)
                db_session.commit()
                flash('New post submitted.')
            return redirect(url_for('display_posts'))
    return render_template('add_post.html', post=post, edit=edit)
Example #2
0
def add_post(id):
    edit = True if id != None else False
    if edit and Post.query.get(id) == None:
        abort(404)
    if edit:
        post = Post.query.get(id)
    else:
        post = None
    if request.method == 'POST':
        if not edit:
            post = Post(request.form['title'], request.form['content'], [])
        if not (request.form['title'] and request.form['content']):
            flash('Both title and content need to be specified.')
            return render_template('add_post.html', post=post, edit=edit)
        else:
            if edit:
                post.title = request.form['title']
                post.content = request.form['content']
                db_session.commit()
                flash('Post edited.')
            else:
                db_session.add(post)
                db_session.commit()
                flash('New post submitted.')
            return redirect(url_for('display_posts'))
    return render_template('add_post.html', post=post, edit=edit)
Example #3
0
def addpost():
    form = PostForm(request.form)
    if request.method == 'POST' and form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        tags = normalize_tags(form.tags.data)
        entry = Post(title=title, content=content)
        entry.tags = tags
        try:
            entry.store_to_db()
            flash(u'文章存入成功')
        except:
            flash(u'文章存入失败,请与管理员联系')

        return redirect(url_for('admin.index'))
            
    return render_template('admin/addpost.html', form=form)
Example #4
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    content=form.content.data,
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash(f'Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html',
                           title='New Post',
                           form=form,
                           legend='New Post')