def comment(post_id): if request.method == "POST": form = CommentForm(request.form) if form.validate_on_submit(): comment = Comment(post_id, session['username'], form.body.data) db.session.add(comment) db.session.flush() db.session.commit() if comment.id: return " Success! Comment added" else: return "Insert failed." else: print("not valid") print(form.body) return "failed validation" else: post = Post.query.filter_by(id=post_id).first() form = CommentForm() if post: #return render_template('/blog/comment.html', form=form, post=post, username=username, action="new") return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Post not found" return "exit comment function"
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() form = CommentForm() if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() body = form.body.data comment = Comment(author, post, body) db.session.add(comment) db.session.commit() flash("Comment posted") return redirect(url_for('article', slug=post.slug)) return render_template('blog/article.html', post=post, form=form)
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id) if form.validate_on_submit(): body = form.comment.data post_id = post.id comment = Comment(body, post_id) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', post=post, form=form, comments=comments, slug=slug)
def add_comment_to_post(request, post_id): post = get_object_or_404(Post, id=post_id) author = request.user if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = author comment.save() return redirect("/index/", post_id=post.id) else: form = CommentForm() return render(request, "add_comment_to_post.html", {'form' : form})
def comment(post_id): form = CommentForm() post = Post.query.filter_by(id=post_id).first_or_404() if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() body = form.body.data comment = Comment(post, author, body) db.session.add(comment) db.session.flush() db.session.commit() flash("Blog post commented") return redirect(url_for('article', slug=post.slug)) return render_template('blog/comment.html', form=form, post=post)
def article(slug): form = CommentForm() if form.validate_on_submit(): post = Post.query.filter_by(slug=slug).first_or_404() author = Author.query.filter_by(username=session['username']).first() body = form.body.data timestamp = datetime.utcnow() comment = Comment(post, author, body, timestamp) db.session.add(comment) db.session.commit() flash("Comment posted.") return redirect(url_for('article', slug=post.slug)) post = Post.query.filter_by(slug=slug).first_or_404() return render_template('blog/article.html', form=form, post=post)
def post_detail_view(request, pk): posts = BlogPost.objects.all() all_tags = Tag.objects.all() post = BlogPost.objects.filter(pk=pk).first() next_post = BlogPost.objects.filter(pk=pk + 1).first() previous_post = BlogPost.objects.filter(pk=pk - 1).first() commentform = CommentForm() if request.method == 'POST': commentform = CommentForm(request.POST) if commentform.is_valid(): pre_save_form = commentform.save(commit=False) pre_save_form.post = post pre_save_form.save() commentform = CommentForm() messages.success(request, 'your comment was saved successfully') context = { 'post': post, 'previous_post': previous_post, 'next_post': next_post, 'posts': posts, 'all_tags': all_tags, 'commentform': commentform } return render(request, 'blog/single-blog1.html', context)
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() form = CommentForm() if form.validate_on_submit(): comment_author = form.comment_author.data comment_body = form.comment_body.data comment = Comment(comment_author, comment_body) form.populate_obj(comment) db.session.add(comment) db.session.commit() post.comment_id = comment.id post.comments = Comment.query.filter_by(id=post.comment_id).first() db.session.add(post) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', form=form, post=post)
def article(post_id): post = Post.query.filter_by(id=post_id).first_or_404() form = CommentForm() if form.validate_on_submit(): comment_author = form.comment_author.data comment_body = form.comment_body.data post_id = post.id comment = Comment(post_id, comment_author, comment_body) db.session.add(comment) # db.session.commit() post.comments.append(comment) db.session.add(post) db.session.commit() return redirect(url_for('article', post_id=post.id)) comments=Comment.query.all() return render_template('blog/article.html', form=form, post=post, comments=comments)
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() if form.validate_on_submit(): author_name = session.get('username') if not author_name: author_name = 'Anonymous' comment = Comment(form.text.data, author_name, post) db.session.add(comment) db.session.flush() db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', post=post, form=form)
def article(slug): form = CommentForm() blog = Blog.query.first() post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id).all() if form.validate_on_submit(): if session['username']: user = User.query.filter_by(username=session['username']).first() comment = Comment( post.id, user, form.content.data ) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) return render_template('blog/article.html', slug=slug, blog=blog, post=post, form=form, comments=comments)
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() replies = post.reply.filter_by(is_live=True).all() comments = post.comment.filter_by(is_live=True).all() return render_template('/blog/article.html', post = post, comments = comments, form = form, replies=replies)
def blog_detail(request, pk): post = Post.objects.get(pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = Comment(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() comments = Comment.objects.filter(post=post) context = { "post": post, "comments": comments, "form": form, } return render(request, "blog_detail.html", context)
def comment(post_id): if request.method == "POST": form = CommentForm(request.form) if form.validate_on_submit(): comment = Comment(post_id, session['username'], form.body.data) #, None) db.session.add(comment) db.session.flush() db.session.commit() if comment.id: return redirect(url_for('getslug', id=post_id)) else: flash("Insert failed.") post = Post.query.filter_by(id=post_id).first() if post: return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Insert failed." else: print("not valid") flash("failed validation") post = Post.query.filter_by(id=post_id).first() if post: return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "failed validation" else: post = Post.query.filter_by(id=post_id).first() form = CommentForm() if post: #return render_template('/blog/comment.html', form=form, post=post, username=username, action="new") return render_template('/blog/comment.html', form=form, post=post, action="new") else: return "Post not found"
def article(slug): user_logged = False if session.get('username'): print('Yes, there is a user logged') user_logged = True form = CommentForm() if form.validate_on_submit(): blog = Blog.query.first() post = Post.query.filter_by(slug=slug).first_or_404() author = Author.query.filter_by(username=session['username']).first() username = session['username'] body = form.body.data comment = Comment(blog, post, author, username, body) db.session.add(comment) db.session.commit() flash("Comment posted.") return redirect(url_for('article', slug = post.slug)) post = Post.query.filter_by(slug = slug).first_or_404() return render_template('blog/article.html', form=form, post=post)
def get_details(request, blog_id): try: blog = Blog.objects.get(id=blog_id) except Blog.DoesNotExist: raise Http404 if request.method == 'GET': form = CommentForm() else: form = CommentForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data cleaned_data['blog'] = blog Comment.objects.create(**cleaned_data) ctx = { 'blog': blog, 'comments': blog.comment_set.all().order_by('-created'), 'form': form } return render(request, 'blog_details.html', ctx)
def post(request, pk): post = get_object_or_404(Post, pk=pk) form = CommentForm() if request.method == 'POST': form = CommentForm(request.POST, author=request.user, post=post) if form.is_valid(): form.save() return HttpResponseRedirect(request.path) return render(request, "blog/post.html", {"post":post, "form":form})
def view_blog(request, year, month, day, post): username = request.session.get('username') detail_post = get_object_or_404(Post, status='published', slug=post, publish__year=year, publish__month=month, publish__day=day) # '''For Comments section''' all_comments = detail_post.comments.all() csubmit = False if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = detail_post new_comment.save() csubmit = True else: form = CommentForm() return render( request, 'blog/blog.html', { 'post': detail_post, 'form': form, 'comments': all_comments, 'csubmit': csubmit, 'username': username })
def post_detail(request, post_id): post = get_object_or_404(Post, id=post_id) comment = Comment.objects.filter(post=post) categ = Category.objects.all() return render( request, 'post_detail.html', { 'post': post, 'comments': comment, "form": CommentForm(), "user": request.user, 'categories': categ, 'media_base': settings.MEDIA_URL })
def article(slug): form = CommentForm() post = Post.query.filter_by(slug=slug).first_or_404() # either find the post or return 404 comments = Comment.query.filter_by(post_id=post.id, live=True).order_by(Comment.comment_date.desc()) # check if comments exist comment_check = Comment.query.first() if not comment_check: is_comment = 0 else: is_comment = 1 # posting a comment error = None if form.validate_on_submit(): author = Author.query.filter_by(username=session['username']).first() comment_body = form.comment_body.data comment = Comment(comment_body, post.id, author.id) db.session.add(comment) db.session.commit() flash("Comment posted!") return render_template('blog/article.html', post=post, comments=comments, form=form, is_comment=is_comment)
def comment_get_detail(request, blog_id): try: blog = Blog.objects.get(id=blog_id) except: raise Http404 if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): cleaned_data = form.cleaned_data # 清洗数据,形成字典 cleaned_data['blog'] = blog # 加入blog信息 Comment.objects.create(**cleaned_data) # 加入Comment对象 return HttpResponseRedirect('/detail/%s/' % blog_id) else: form = CommentForm() info_data = { 'blog': blog, 'comments': blog.comment_set.all().order_by('-pub'), #comment_set是django中对外键的调用 'form': form, } return render(request, 'comment_submit.html', info_data)
def article(slug): post = Post.query.filter_by(slug=slug).first_or_404() comments = Comment.query.filter_by(post_id=post.id) form = CommentForm() if request.method == 'POST': if session.get('username'): if form.validate_on_submit(): blog = Blog.query.first() author = Author.query.filter_by( username=session['username']).first() post = Post.query.filter_by(slug=slug).first_or_404() body = form.body.data comment = Comment(author, blog, post, body) db.session.add(comment) db.session.commit() return redirect(url_for('article', slug=slug)) else: flash('Please fill out required fields') else: flash('You must be logged in to comment.') return render_template('blog/article.html', post=post, comments=comments, form=form)
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blog/comment_form.html', {'form': form})
def blog_detail(request, year, month, day, id): article = get_object_or_404(Article, pk=id) img_id = UserInfo.objects.get(id=1) str = img_id.img.url comments = Comment.objects.all() if request.method == 'GET': # 如果请求的方式是get 生产一个空的表单对象,通过上下文直接返回 form = CommentForm() Context = { 'detail_list': article, 'img_index': str, 'form': form, 'comments': comments } return render(request, 'blog_datail.html', Context) elif request.method == 'POST': # 如果是post方式,那么先创建一个表单实例然后绑定数据到该表单,然后通过表单的is_valid()方法对form进行数据有效性性检验, form = CommentForm(request.POST) if form.is_valid(): data = form.cleaned_data Comment.objects.create(article=article, text=data['comments'], name=data['name'], email=data['email']) # 这里再次创建一个空的表单对象是因为,上一条的新增数据语句已经执行成功,再次生成空的对象是为了防止网页上新增成功之后 # 数据依然残留在表单中的bug,(还有其他方式,这并不是一种最完美的解决方案) form = CommentForm() Context = { 'detail_list': article, 'img_index': str, 'form': form, 'comments': comments } messages.success(request, '留言成功!') return render(request, 'blog_datail.html', Context) else: # 如果数据的有效性建议并没有通过,同上一样,直接返回 Context = { 'detail_list': article, 'img_index': str, 'form': form, 'comments': comments } messages.error(request, '留言失败') return render(request, 'blog_datail.html', Context)
def add_comment_to_post(request, post_id): post = get_object_or_404(Post, id=post_id) author = request.user if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.author = author comment.save() return redirect("/index/", post_id=post.id) else: form = CommentForm() return render(request, "add_comment_to_post.html", {'form': form})