Ejemplo n.º 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)
Ejemplo n.º 2
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')