def create_post(): form = BlogPostForm() if form.validate_on_submit(): if form.blog_image.data: pic = add_blog_pic(form.blog_image.data) pic_thumbnail = create_thumbnail(form.blog_image.data) BlogPost.blog_image = pic blog_post = BlogPost(title=form.title.data, text=form.text.data, category=form.category.data, user_id=current_user.id, blog_image=pic, blog_thumbnail=pic_thumbnail) else: blog_post = BlogPost(title=form.title.data, text=form.text.data, category=form.category.data, user_id=current_user.id, blog_image='c1.jpg', blog_thumbnail='stories1.jpg') db.session.add(blog_post) db.session.commit() flash("Blog Post Created") return redirect(url_for('core.index')) return render_template('create_post.html', form=form)
def create_post(): form = BlogPostForm() if form.validate_on_submit(): blog_post = BlogPost(form.title.data, form.text.data, current_user.id) db.session.add(blog_post) db.session.commit() flash('Blog Post Created') return redirect(url_for('core.index')) return render_template('create_post.html', form=form)
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") return redirect(url_for("core.index")) return render_template("create_post.html", form=form)
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() return redirect(url_for('blog_posts.blog_post',blog_post_id=blog_post.id)) return render_template('create_post.html',form=form)
def create_post(): form = BlogPostForm() if form.validate_on_submit(): blog_post = BlogPost(title=form.title.data, # Create a sample Blog of BlogPost model text=form.text.data, user_id=current_user.id) db.session.add(blog_post) db.session.commit() flash('Blog Created') return redirect(url_for('core.index')) # Redirect to Home page return render_template('create_post.html', form=form)
def create_post(): form = BlogPostForm() print("create the form") if form.validate_on_submit(): print("submit it") 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')) return render_template('create_post.html', form=form)
def create_post(): form = BlogPostForm() if form.validate_on_submit(): post = BlogPost(title=form.title.data, text=form.text.data, user_id=current_user.id) db.session.add(post) db.session.commit() flash('Post created successfully!') return redirect(url_for('core.home')) return render_template('create_post.html', form=form)
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) #we need to grab the user id from our BlogPost model from the current user because #form does not ask for id db.session.add(blog_post) db.session.commit() flash("Blog Post Created") return redirect(url_for('core.index')) return render_template('create_post.html', form=form)
def create_post(): form = BlogPostForm() if form.validate_on_submit(): # Создаем ОБЪЕКТИ МОДЕЛИ BlogPost() - Берутся поля заполнения БД title и text (id и date заполняются автоматически) # + УСТАНАВЛИВАЕТСЯ СВЯЗЬ С User() через user_id = db.Column(db.Integer,db.ForeignKey('users.id'), nullable=False). т.к. user_id ЕСТЬ в __init__ # Тоесть создавая запись в блоге сразу связывает её с Авторизированным в данную СЕССИЮ пользователем blog_posts = BlogPost(title=form.title.data, text=form.text.data, user_id=current_user.id) #Добвляем созданный объект в БД db.session.add(blog_posts) db.session.commit() flash('Post Created') return redirect(url_for('core.index')) return render_template('create_post.html', form=form)
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() return redirect(url_for('core.index')) return render_template('create_post.html', form=form) # Views @blog_posts.route('/<int:blog_post_id>') def blog_post(blog_post_id): blog_post = BlogPost.query.get_or_404(blog_post_id) return render_template('blog_post.html', title=blog_post.title, date=blog_post.date, post=blog_post)
def create_post(): # BlogPostFormを配置 form = BlogPostForm() # submitボタンが押された場合 if form.validate_on_submit(): #form.title.dataのようにフォームに入力された値を取り出す # models.pyのBlogPostデータベースモデルのインスタンスを作成する 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') # redirectは別のurlへ飛ばす # ここではcoreブループリントのindex関数へ飛ばす return redirect(url_for('core.index')) # create_post.htmlを表示する。BlogPostForm()のformインスタンスを渡して入力してもらう return render_template('create_post.html', form=form)
def user_posts(username): page = request.args.get("page",1,type=int) user = User.query.filter_by(username=username).first_or_404() blog_posts = BlogPost.query.filter_by(author=user).order_by(BlogPost.desc()).paginate(page=page,per_page=5) return render_template("user_blog_posts.html",blog_posts=blog_posts,user=user)