def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data subtitle = form.subtitle.data theme = request.files.getlist('image')[0].filename body = form.body.data category = Category.query.get(form.category.data) topic = Topic.query.get(form.topic.data) post = Post(title=title, subtitle=subtitle, theme=theme, body=body, category=category, topic=topic) # same with: # category_id = form.category.data # post = Post(title=title, body=body, category_id=category_id) db.session.add(post) db.session.commit() img_list = request.files.getlist('image') img_path = current_app.root_path + '/static/img/' + str(topic.name) if img_list[0].filename: for f in img_list: filename = f.filename f.save(os.path.join(img_path, filename)) flash('Post created.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def edit_post(post_id): form = PostForm() post = Post.query.get_or_404(post_id) if form.validate_on_submit(): post.title = form.title.data post.subtitle = form.subtitle.data post.body = form.body.data post.update_time = datetime.utcnow() post.category = Category.query.get(form.category.data) post.topic = Topic.query.get(form.topic.data) db.session.commit() img_path = current_app.root_path + '/static/img/' + str( post.topic.name) for f in request.files.getlist('image'): filename = f.filename f.save(os.path.join(img_path, filename)) flash('Post updated.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) form.title.data = post.title form.subtitle.data = post.subtitle form.body.data = post.body form.category.data = post.category_id form.topic.data = post.topic_id return render_template('admin/edit_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category) db.session.add(post) db.session.commit() flash("post created", "success") return redirect(url_for("blog.show_post", post_id=post.id)) return render_template("admin/new_post.html", form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data category = Category.query.get(form.category.data) body = form.body.data post = Post(title=title, category=category, body=body) db.session.add(post) db.session.commit() flash('文章已创建', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def edit_post(post_id): post = Post.query.get(post_id) form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.category = Category.query.get(form.category.data) db.session.commit() flash("edit success!", "success") return redirect(url_for("blog.show_post", post_id=post.id)) form.category.data = post.category_id form.title.data = post.title form.body.data = post.body return render_template("admin/edit_post.html", form=form)
def edit_post(post_id): form = PostForm() post = Post.query.get_or_404(post_id) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.category = Category.query.get(form.category.data) db.session.commit() flash('Post updated.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) form.title.data = post.title form.body.data = post.body form.category.data = post.category_id return render_template('admin/edit_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data private = form.private.data # category = Category.query.get(form.category.data) categories = [Category.query.get(i) for i in form.categories.data] post = Post(title=title, body=body, private=private, categories=categories) db.session.add(post) db.session.commit() flash("Post created.", "success") return redirect(url_for("blog.show_post", post_id=post.id)) return render_template("admin/new_post.html", form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): title = form.title.data body = form.body.data category = Category.query.get(form.category.data) post = Post(title=title, body=body, category=category, read=1) # same with: # category_id = form.category.data # post = Post(title=title, body=body, category_id=category_id) db.session.add(post) db.session.commit() flash('Post created.', 'success') return redirect(url_for('blog.show_post', post_id=post.id)) return render_template('admin/new_post.html', form=form)
def new_post(): form = PostForm() if form.validate_on_submit(): #save it into database post = Post(title=form.title.data, content=form.content.data, author=current_user) db.session.add(post) db.session.commit() #end flash('your post has been creatd', 'success') return redirect(url_for('home')) return render_template('create_post.html', title='Post', form=form, legend='Post')
def NewPost(): form = PostForm() if form.validate_on_submit(): pst = Post(title=form.title.data, content=form.content.data, smallcontent=form.smallcontent.data, author=current_user) db.session.add(pst) db.session.commit() flash(f'Post Added Succecfully', 'success') return redirect(url_for('UserProfile', user_name=current_user.username)) return render_template("NewPost.html", form=form, title="Add A New Post", leg="Add New Post", user=current_user)
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.title = form.title.data post.content = form.content.data db.session.commit() flash('your post has been updated', 'success') return redirect(url_for('post', post_id=post.id)) elif request.method == 'GET': form.title.data = post.title form.content.data = post.content return render_template('create_post.html', title='Update Post', form=form, legend='Update Post')
def edit_post(post_id): form = PostForm() post = Post.query.get_or_404(post_id) if form.validate_on_submit(): post.title = form.title.data post.body = form.body.data post.private = form.private.data # post.category = Category.query.get(form.category.data) categories = [Category.query.get(i) for i in form.categories.data] post.categories = categories db.session.commit() flash("Post <%s> updated." % post.title, "success") return redirect(url_for("blog.show_post", post_id=post.id)) form.title.data = post.title form.body.data = post.body form.private.data = post.private form.categories.data = [category.id for category in post.categories] return render_template("admin/edit_post.html", form=form)
def UpdatePost(post_id): post = Post.query.get(post_id) if (post.author != current_user): abort(403) else: form = PostForm() if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data post.smallcontent = form.smallcontent.data db.session.commit() flash(f'Post Updated Succecfully', 'success') return redirect(url_for('FullPost', post_id=post.id)) elif request.method == "GET": form.title.data = post.title form.smallcontent.data = post.smallcontent form.content.data = post.content return render_template("NewPost.html", form=form, title="Update Post :" + str(post_id), leg="Update Poste", user=current_user)