示例#1
0
def create_post():
    form = BlogPostForm()

    if form.validate_on_submit():
        blog_post = BlogPost(title=form.title.data,
                             text=form.text.data,
                             user_id=current_user.id)

        db.session.add(blog_post)
        db.session.commit()

        flash('Blog post created!', 'success')
        return redirect(url_for('core.index'))

    return render_template('create_post.html', form=form)
def create_post():
    form = BlogPostForm()

    if form.validate_on_submit():
        # INSTANCE OF BLOGPOST
        blog_post = BlogPost(title=form.title.data,
                             text=form.text.data,
                             user_id=current_user.id)
        # SAVE TO DATABASE
        db.session.add(blog_post)
        db.session.commit()
        flash('Post Created')
        return redirect(url_for('core.index'))

    return render_template('create_post.html', form=form)
示例#3
0
def update_post(blog_post_id):
    blog_post = BlogPost.query.get_or_404(blog_post_id)
    # to check if the visitor is the actual author
    if blog_post.author != current_user:
        abort(403)

    form = BlogPostForm()
    if form.validate_on_submit():
        blog_post.title = form.title.data
        blog_post.text = form.text.data
        db.session.commit()
        flash('Blog post updated')
        return redirect(url_for('blog_posts.blog_post', blog_post_id=blog_post.id))
    elif request.method == 'GET':
        form.title.data = blog_post.title
        form.text.data = blog_post.text
    return render_template('create_post.html', title='Updating', form=form)
示例#4
0
def update(blog_post_id):
    blog_post = BlogPosts.query.get_or_404(blog_post_id)
    if blog_post.author != current_user:
        # Forbidden, No Access
        abort(403)

    form = BlogPostForm()
    if form.validate_on_submit():
        blog_post.title = form.title.data
        blog_post.text = form.text.data
        db.session.commit()
        flash('Post Updated')
        return redirect(
            url_for('blog_posts.blog_post', blog_post_id=blog_post.id))
    # Pass back the old blog post information so they can start again with
    # the old text and title.
    elif request.method == 'GET':
        form.title.data = blog_post.title
        form.text.data = blog_post.text
    return render_template('create_post.html', title='Update', form=form)
示例#5
0
def create_post():
    form = BlogPostForm()
    print("Came here")

    if form.validate_on_submit():
        print("I qm Here")
        blog_post = BlogPost(title = form.title.data,
                            text = form.text.data,
                            user_id = current_user.id)
        db.session.add(blog_post)
        db.session.commit()
        flash('Blog Post Created')
        return redirect(url_for('core.index'))

    elif request.method == 'GET':
        print("This error")

    else:
        print("errored")
    
    return render_template('create_post.html',form = form)
def update(blog_post_id):
    post = BlogPost.query.get_or_404(blog_post_id)
    if post.author != current_user:
        abort(403)

    form = BlogPostForm()

    if form.validate_on_submit():
        post.title = form.title.data
        post.text = form.text.data

        db.session.add(post)
        db.session.commit()

        flash('Blog Post Updated')

        return redirect(url_for('blog_posts.blog_post', blog_post_id=post.id))
    else:
        # if hitting the page for the first time
        if request.method == 'GET':
            form.title.data = post.title
            form.text.data = post.text

    return render_template('create_post.html', form=form, title='Update Post')