def add_comment(request,article_id): a = Article.objects.get(id=article_id) if request.method == "POST": f =CommentForm(request.POST) if f.is_valid(): # This means that save this instance of form but do not # push anything to databases(commit = False) as we are # not finished yet c = f.save(commit =False) c.pub_date = timezone.now() c.article = a c.save() # by default commit = True, therefore the comment will be # saved to the database. return HttpResponseRedirect('/articles/get/%s' % article_id ) else: f = CommentForm() args = {} args.update(csrf(request)) args['article'] = a args['form'] = f return render_to_response('add_comment.html',args)
def post(request, post_slug): post = Post.objects.get(slug=post_slug) user = request.user form = CommentForm(request.POST or None) if request.method == "POST": if form.is_valid(): temp = form.save(commit=False) temp.post = post temp.user = user parent = form['parent'].value() print parent if parent == '': # Set a blank path then save it to get an ID temp.path = [] temp.save() temp.path = [temp.id] else: # Get the parent node node = Comment.objects.get(id=parent) temp.depth = node.depth + 1 temp.path = node.path # Store parents path then apply comment ID temp.save() temp.path.append(temp.id) # Final save for parents and children temp.save() form = CommentForm() # Reset the form, clean the fields. (AJAX doesn't refresh the page) # Retrieve all comments and sort them by path comment_tree = Comment.objects.filter(post=post).order_by('path') return render(request, 'reddit/post.html', locals())
def play(id, page=None): if page == None: page = 1 form = CommentForm() # movie =Movie.query.get(int(id)) movie = Movie.query.join(Tag).filter(Tag.id == Movie.tag_id, Movie.id == int(id)).first() movie.playnum = movie.playnum + 1 db.session.commit() # 分页查询 page_data = Comment.query.join(User).join(Movie).filter( User.id == Comment.user_id, Movie.id == movie.id).order_by( Comment.addtime.desc()).paginate(page=page, per_page=5) if 'user' in session and form.validate_on_submit(): data = form.data comment = Comment(content=data['content'], movie_id=movie.id, user_id=session['user_id']) db.session.add(comment) db.session.commit() flash('评论添加成功', 'ok') movie.commentnum = movie.commentnum + 1 db.session.add(movie) db.session.commit() return redirect(url_for('home.play', page=page, id=id)) return render_template("home/play.html", movie=movie, form=form, page_data=page_data)
def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() return redirect('/feed/')
def main(): form = CommentForm(request.form) table = Comments(get_comments()['content']) table.border = True if request.method == 'POST' and form.validate(): # save the album add_comment(form) flash('Comment created successfully!') return redirect('/') return render_template('index.html', form=form, table=table)
def addcomment(): form = CommentForm() error = 'Sorry, Post Comments Error!' if form.validate_on_submit(): comment = Comment(author_ip=request.environ['HTTP_X_FORWARDED_FOR']) form.populate_obj(comment) db.session.add(comment) post = Post.query.getpost_id(comment.post_id) post.comment_count += 1 db.session.commit() return redirect(url_for('article', postid=comment.post_id)) return render_template('/error.html', content=error)
def post(id): # Detail 详情页 post = Post.query.get_or_404(id) # 评论窗体 form = CommentForm() # 保存评论 if form.validate_on_submit(): comment = Comment(body=form.body.data, post=post) db.session.add(comment) db.session.commit() return render_template('posts/detail.html', title=post.title, form=form, post=post)
def detail(request,article_id): try: article = models.Article.objects.get(pk=article_id)#pk=id comment_list = models.Comment.objects.all() except models.Article.DoesNotExist: raise Http404 if request.method=='POST': form = CommentForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/blog/%s/'%article_id) else: form = CommentForm() return render_to_response('detail.html',{'article':article,'comment_list':comment_list,'form':form})
def article_byname(postname): categorys = Category.query.getall() hot = Post.query.hottest()[:20] new = Post.query.newpost()[:20] tag = Tag.query.getall() shuffle(tag) tag = tag[:20] comments = Comment.query.getall()[:20] articles = Post.query.getall() shuffle(articles) articles = articles[:5] post = Post.query.getpost_byname(postname) if not post: return redirect(url_for('error_404')) form = CommentForm() postcoments = post.comments.all() post.view_num += 1 db.session.commit() return render_template('/post.html', post=post, articles=articles, categorys=categorys, hotarticles=hot, newpost=new, tags=tag, comments=comments, postcoments=postcoments, form=form)
def article(postid=5): categorys = Category.query.getall() hot = Post.query.hottest()[:20] new = Post.query.newpost()[:20] tag = Tag.query.getall() shuffle(tag) tag = tag[:20] comments = Comment.query.newcomment()[:20] articles = Post.query.getall() shuffle(articles) articles = articles[:5] post = Post.query.get_or_404(postid) form = CommentForm() postcoments = post.comments.all() post.view_num += 1 db.session.commit() return render_template('/post.html', post=post, articles=articles, categorys=categorys, hotarticles=hot, newpost=new, tags=tag, comments=comments, postcoments=postcoments, form=form)
def show_post(post_id): form = CommentForm() requested_post = Post.query.get(post_id) if form.validate_on_submit(): if not current_user.is_authenticated: flash("You need to login or register to comment.") return redirect(url_for("login")) new_comment = Comment(text=form.comment_text.data, comment_author=current_user, parent_post=requested_post) db.session.add(new_comment) db.session.commit() return render_template('post.html', post=requested_post, current_user=current_user, form=form)
def comment(request): form = CommentForm(request.POST) if form.is_valid(): comment = Comment() comment.content = form.cleaned_data.get('content') comment.user = request.user id=form.cleaned_data.get('article_id') try: article = Article.objects.get(id=id) comment.article = article comment.save() article.save() rtn = {'status':True,'redirect':reverse('blog:show', args=[id])} except Article.DoesNotExist: rtn = {'status':False,'error':'Article is not exist'} else: rtn = {'status':False,'error':form.errors} return JsonResponse(rtn)
def comment(): form = CommentForm() pid = int(request.args.get("post")) post = Post.query.filter(Post.pid == pid).first() if not post: return error("That post does not exist!") comment = Comment(content=form.content.data, post_time=datetime.utcnow(), pid=pid) current_user.comments.append(comment) post.comments.append(comment) db.session.commit() return redirect("/viewpost?post=" + str(pid))
def addpark(): """Form & Page to add a park, if not logged in or signed up, have a pop-up login""" DEFAULT_PARK_PIC = "/static/images/defaultPark.jpg" all_parks = ParkPost.query.all() comment = CommentForm() park = UserParkInput() ################################# # Add Comment Coming soon! # if comment.validate_on_submit(): # try: # comment_post = Comments(comment=comment.comment.data) # db.session.add(comment_post) # db.session.commit() # except IntegrityError: # flash("Check input", "danger") # return render_template('addpark.html', comment=comment, park=park, all_parks=all_parks) # flash("Comment added!", "success") # return redirect('/addpark') # return render_template('addpark.html', comment=comment, park=park, all_parks=all_parks) ################################ # Adding A Park if park.validate_on_submit(): try: park_post = ParkPost( park_name=park.park_name.data, description=park.description.data, address=park.address.data, image=park.image.data or DEFAULT_PARK_PIC ) db.session.add(park_post) db.session.commit() except IntegrityError: flash("Check input", "danger") return render_template('addpark.html', comment=comment, park=park, all_parks=all_parks) # show all park posts flash("Park added!", "success") return redirect('/addpark') return render_template('addpark.html', comment=comment, park=park, all_parks=all_parks)
def show(request,id): article = get_object_or_404(Article,id=id) title = 'Show' form = CommentForm(initial={'article_id':article.id}) return render(request, 'blog/show.html',{'article':article,'title':title,'form':form})