コード例 #1
0
def blog():
    form = PostForm()
    posts = Post.query.order_by(Post.date_posted.desc()).all()
    image_file = url_for('static', filename='img/' + current_user.image_file)
    if form.validate_on_submit():

        if form.image.data:
            picture_img = save_image(form.image.data)

            post = Post(title=form.title.data,
                        content=form.content.data,
                        image=picture_img,
                        author=current_user)
        else:
            post = Post(title=form.title.data,
                        content=form.content.data,
                        author=current_user)
        db.session.add(post)
        db.session.commit()
        flash("YOur post has been created!", "success")
        return redirect(url_for('blog'))

    return render_template("blog.html",
                           active=active,
                           form=form,
                           posts=posts,
                           image_file=image_file)
コード例 #2
0
def upload():
    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("YOur post has been created!", "success")
        return redirect(url_for('home'))

    return render_template("upload.html",
                           active=active,
                           form=form,
                           legend="New Post")
コード例 #3
0
def update_post(post_id):
    post = Post.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.caption = form.title.data
        db.session.commit()
        flash("Your post has been update!", "success")
        return redirect(url_for('post', post_id=post.id))
    elif request.method == "GET":
        form.title.data = post.caption
    return render_template('post_single.html',
                           post=post,
                           form=form,
                           legend="Update post",
                           active=active)
コード例 #4
0
def blog_single(post_id):
    form = PostForm()
    post = Post.query.get_or_404(post_id)

    return render_template('blog_single.html',
                           post=post,
                           form=form,
                           active=active)
コード例 #5
0
def profile(username, id):
    form = PostForm()

    user = User.query.filter_by(username=username).first_or_404()
    id = User.query.get(id)
    posts = Post.query.filter_by(author=id).order_by(
        Post.date_posted.desc()).all()
    user_img = url_for('static', filename='img/' + user.image_file)

    image_file = url_for('static', filename='img/' + current_user.image_file)
    return render_template('profile.html',
                           user=user,
                           id=id,
                           posts=posts,
                           image_file=image_file,
                           form=form,
                           active=active)
コード例 #6
0
def comment():
    form = PostForm()
    com = Post(comment=form.title.data, author=current_user)
    db.session.add(com)
    db.session.commit()
    return redirect("home")