def post(post_id): """Post detail page.""" form = CommentForm() if form.validate_on_submit(): # If valid form is submitted, add comment and redirect back to page new_comment = Comment() new_comment.name = form.name.data new_comment.text = form.text.data new_comment.post_id = post_id new_comment.date = datetime.datetime.now() db.session.add(new_comment) db.session.commit() return redirect(request.url) else: # No form submitted or form is invalid, show post (with errors if appropriate) post = Post.query.get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template( 'post.html', post=post, tags=tags, comments=comments, recent=recent, top_tags=top_tags, form=form, )
def blogContent(blog_name): blog = Blog.query.filter_by(title=blog_name).first() blog.click = blog.click + 1 db.session.add(blog) db.session.commit() original = Blog.query.filter_by().count() fans = User.query.filter_by().count() comments_total = Comment.query.filter_by().count() java_blog = Blog.query.filter_by(tag='Java').count() c_blog = Blog.query.filter_by(tag='C').count() flask_blog = Blog.query.filter_by(tag='Flask').count() opengl_blog = Blog.query.filter_by(tag='OpenGL').count() android_blog = Blog.query.filter_by(tag='Android').count() python_blog = Blog.query.filter_by(tag='Python').count() form = CommentForm() blog_path = 'blogs/' + blog_name + '.html' comments = [] comments_in_db = Comment.query.filter(Comment.blog_id == blog.id).all() for comment in comments_in_db: icon_index = str(random.randint(1, 6)) username = User.query.filter(User.id == comment.user_id).first() comments.append({ 'username': username.username, 'information': comment.information, 'icon': icon_index }) log_in_username = session.get("USERNAME") if log_in_username: if form.validate_on_submit(): user_in_db = User.query.filter( User.username == session.get('USERNAME')).first() comment = Comment(information=form.comment.data, blog_id=blog.id, user_id=user_in_db.id) db.session.add(comment) db.session.commit() form.comment.data = "" return redirect(url_for('blogContent', blog_name=blog_name)) else: flash("You have not log in yet!") return render_template('blogContent.html', name=blog_path, form=form, comments_in_db=comments, original=original, fans=fans, comments_total=comments_total, java_blog=java_blog, c_blog=c_blog, flask_blog=flask_blog, opengl_blog=opengl_blog, android_blog=android_blog, python_blog=python_blog, username=log_in_username)
def mutate(root, info, input=None): Posts = [] for post_input in input.posts: post = Post.objects.get(pk=post_input['id']) if post is None: return CreateComment(comment=None) Posts.append(post) comment_instance = Comment(text=input.text, author=input.author) comment_instance.save() comment_instance.posts.set(Posts) return CreateComment(comment=comment_instance)
def publishcomment(request): ##使用一个form类 content = request.POST['comment'] blogid = request.POST['blogid'] commentModel = Comment() commentModel.content = content commentModel.blogid_id = blogid commentModel.save() blogModelList = Blog.objects.all() return render(request, 'blogapp/publishblog.html', {'blogModelList': blogModelList})
def detail(request,page_num): if request.method == "GET": form = CommentForm if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Aritcle.objects.get(id=page_num) c = Comment(name = name,comment = comment,belong_to=a) c.save() return redirect(to=detail,page_num=page_num) context = {} #comment_list = Comment.objects.all() article = Aritcle.objects.get(id=page_num) context['article'] = article #context['comment_list'] = comment_list context['form'] = form return render(request, 'detail.html', context)