def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['comments'] = Comment.objects.prefetch_related(self.positive_vote_prefetch)\ .prefetch_related(self.negative_vote_prefetch).filter(post=self.object) context['current_subforum'] = self.object.subforum context['current_subforum_url'] = self.object.subforum.url_name context['current_forum'] = self.object.subforum.forum context['current_forum_url'] = self.object.subforum.forum.url_name context['comment_form'] = CommentForm(context['object']) context['positive_post_vote_form'] = PostVoteForm( post_id=self.object.id, positive=True) context['negative_post_vote_form'] = PostVoteForm( post_id=self.object.id, positive=False) context['positive_comment_vote_form'] = CommentVoteForm(positive=True) context['negative_comment_vote_form'] = CommentVoteForm(positive=False) return context
def getMsgBoard(request): articles = Article.objects.all().filter( type='LYB').order_by('-created_time') if articles.count() > 0: msg_board = articles[0] comment_list = msg_board.comment_set.all() else: msg_board = None comment_list = None form = CommentForm() return render(request, 'blog/msg_board.html', context={ 'form': form, 'comment_list': comment_list, 'msg_boards': msg_board })
def post_comment(request, post_pk): post = get_object_or_404(Post, pk=post_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) else: comment_list = post.comment_set.all() context = { 'post': post, 'form': form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context) return redirect(post)
def comment(request, post_id): post = get_object_or_404(Post, pk=post_id) form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect(post) context = { 'post': post, 'form': form, } print(context) return render(request, 'comments/preview.html', context=context)
def detail(request, pk): post = get_object_or_404(Post, pk=pk) # 阅读量 +1 post.increase_views() post.body = markdown.markdown(post.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) form = CommentForm() # 获取这篇 post 下的全部评论 comment_list = post.comment_set.all() # 将文章、表单、以及文章下的评论列表作为模板变量传给 detail.html 模板,以便渲染相应数据。 context = {'post': post, 'form': form, 'comment_list': comment_list} return render(request, 'blog/detail.html', context={'post': post})
def showFile(request, pk): file = get_object_or_404(UploadFiles, pk=pk) context = login_utils(request) context['file'] = file context['type'] = str(file.fileType) == "视频" # 关联文件的 content_type = ContentType.objects.get_for_model(file) # 评论 初始化隐藏标签的内容 一个是文件类型 二是id data = {} data["content_type"] = content_type.model data["object_id"] = pk commentForm = CommentForm(initial=data) # 提交评论表单 context["commentForm"] = commentForm # 评论内容 context["comments"] = Comments.objects.filter(object_id=pk) return render(request, 'upload_files/show_content.html', context=context)
def test_show_comment_form_with_invalid_bound_form(self): template = Template( "{% load comments_extras %}" "{% show_comment_form post form %}") invalid_data = { 'email': 'invalid_email' } form = CommentForm(data=invalid_data) self.assertFalse(form.is_valid()) context = Context(show_comment_form( self.context, self.post, form=form)) expected_html = template.render(context) for field in form: label = '<label for="{}">{}:</label>'.format( field.id_for_label, field.label) self.assertInHTML(label, expected_html) self.assertInHTML(str(field), expected_html) self.assertInHTML(str(field.errors), expected_html)
def detail(request, pk): post = get_object_or_404(Post, pk=pk) #阅读量 post = Post.objects.get(pk=pk) post.views = int(post.views + 1) post.save() post.body = markdown.markdown(post.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) #导入CommentForm form = CommentForm() #获取这篇post 下的全部评论 comment_list = post.comments_set.all().order_by('-created_time') context = {'post': post, 'form': form, 'comment_list': comment_list} return render(request, 'detail.html', context=context)
def detail(request, pk): article = get_object_or_404(models.Article, pk=pk) article.increase_views()#阅读量+1 extensions = ['markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ] article.body = markdown.markdown(article.body, extensions) form = CommentForm() # comment_list = models.Article.objects.get(pk=pk).comment_set comment_list = article.comment_set.all() context = { 'post': article, 'form': form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context)
def detail(request, pk): post = get_object_or_404(Post, pk=pk) post.click_rate_add() post.save() post.body = markdown.markdown(post.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) # 记得在顶部导入 CommentForm form = CommentForm() # 获取这篇 post 下的全部评论 comment_list = post.comment_set.all() context = {'post': post, 'form': form, 'comment_list': comment_list} return render(request, 'blog/detail.html', context=context)
def getExplain(request): articles = Article.objects.all().filter( type='SM').order_by('-created_time') if articles.count() > 0: explain = articles[0] comment_list = explain.comment_set.all() else: explain = None comment_list = None form = CommentForm() return render(request, 'blog/explain.html', context={ 'form': form, 'comment_list': comment_list, 'explains': explain })
def detail(request, pk): lyric = get_object_or_404(Lyric, pk=pk) lyric.body = markdown.markdown(lyric.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) # 记得在顶部导入 CommentForm form = CommentForm() # 获取这篇 post 下的全部评论 comment_list = lyric.comment_set.all() # 将文章、表单、以及文章下的评论列表作为模板变量传给 detail.html 模板,以便渲染相应数据。 context = {'lyric': lyric, 'form': form, 'comment_list': comment_list } return render(request, 'gcmaster/detail.html', context=context)
def detail(request, pk): post = get_object_or_404(Post, pk=pk) post.body = markdown.markdown(post.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) #在顶部导入CommentForm form = CommentForm() #获取这篇post的全部评论 comment_list = post.comment_set.all() #将文章、表单以及文章下的评论列表作为模板变量传给detail.html模板,以便渲染响应数据 context = {'post': post, 'form': form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context)
def get(self, request, id): articles = Article.objects.all()[:5] article = get_object_or_404(Article, pk=id) print(article.comment_set.count()) cf = CommentForm() # 处理点击阅读次数 article.views += 1 article.save() # 实例化markdown对象 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc' ]) # 使用markdown渲染制定的字段 article.body = md.convert(article.body) article.toc = md.toc return render(request, "article/single.html", locals())
def get(self, req, id): article = get_object_or_404(Article, pk=id) #1.对指定的字段使用markdown渲染成html格式 md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc' ]) #2. 使用markdown实例渲染指定字段 article.body = md.convert(article.body) #3.将md的目录对象赋予 article article.toc = md.toc #4.进入admin管理员,修改文章为markdown格式,在指定地方使用[TOC]插入目录,然后使用转义 #{{article.body|safe}} {{article.toc|safe}} |safe转义 # #向详情页传递一个评论表单 cf = CommentForm() return render(req, "blog/single.html", locals())
def detail(request, pk): post = get_object_or_404(Post, pk=pk) post.increase_views() post.body = markdown.markdown(post.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) form = CommentForm() comment_list = post.comment_set.all() context = {'post': post, 'form': form, 'comment_list': comment_list} return render(request, 'blog/detail.html', context=context)
def article_detail(request, pk): article = get_object_or_404(Article, pk=pk) article.body = markdown.markdown(article.body, extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) article.increase_reading_quantity() comment_form = CommentForm() comment_list = article.comment_set.all() context = { 'article': article, 'comment_form': comment_form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context)
def detail(request, pk): post = get_object_or_404(Post, pk=pk) # 阅读量 +1 post.increase_views() md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', TocExtension(slugify=slugify) ]) post.body = md.convert(post.body) form = CommentForm() comment_list = post.comment_set.all() context = { 'post': post, 'toc': md.toc, 'form': form, 'comment_list': comment_list } return render(request, 'blog/detail.html', context=context)
def detail(request, pk): index_list = models.Article.objects.get(id=pk) index_list.blog_body = markdown.markdown(index_list.blog_body, extension=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', 'markdown.extensions.fenced_code', 'markdown.extensions.tables' 'markdown.extensions.extensions' ]) form = CommentForm() comment_list = index_list.comment_set.all() context = {'index_list': index_list, 'form': form, 'comment_list': comment_list } return render(request, 'index/index_list.html', context=context)
def post_detail(request, slug=None): instance = get_object_or_404(Post, slug=slug) if instance.publish > timezone.now().date() or instance.draft: if not request.user.is_staff or not request.user.is_superuser: raise Http404 share_string = quote_plus(instance.content) instance_data = { 'content_type': instance.get_content_type, 'object_id': instance.id, } form = CommentForm(request.POST or None, initial=instance_data) if form.is_valid(): c_type = form.cleaned_data.get('content_type') obj_id = form.cleaned_data.get('object_id') content_type = ContentType.objects.get(model=c_type) content_data = form.cleaned_data.get('content') parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: qs = comments.objects.filter(id=parent_id) if qs.exists(): parent_obj = qs.first() new_comment, created = comments.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) Comments = comments.objects.filter_by_instance(instance) context = { "title": instance.title, "instance": instance, "share_string": share_string, "comment_form": form, "comments": Comments } return render(request, "post_detail.html", context)
def ArticleDetailView(request, slug=None): instance = get_object_or_404(Article, slug=slug) initial_data = {"content_type": instance.get_content_type, "object_id": instance.id } form = CommentForm(request.POST or None, initial=initial_data) if form.is_valid() and request.user.is_authenticated(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get("object_id") content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user = request.user, content_type = content_type, object_id = obj_id, content = content_data, parent = parent_obj, ) return HttpResponseRedirect(new_comment.content_object.get_absolute_url()) comments = instance.comments md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite(linenums=True)', 'markdown.extensions.toc', TocExtension(slugify=slugify, permalink=True) ]) instance.body = md.convert(instance.body) context = { "title": instance.title, "instance": instance, "toc": md.toc, "comments": comments, "comment_form": form, } return render(request, 'ccposts/detail.html', context=context)
def feature_detail(request, slug=None): # A view which returns a single Bug object based on the ID(pk) if request.user.is_authenticated: feature = get_object_or_404(Feature, slug=slug) comments = feature.comments initial_data = { "content_type": feature.get_content_type, "object_id": feature.id } form = CommentForm(request.POST or None, initial=initial_data) if form.is_valid(): user = UserProfile.objects.get(user=request.user) c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get("object_id") content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) args = { 'feature': feature, 'comments': comments, 'comment_form': form, } return render(request, "featuredetail.html", args) else: return redirect('index')
def lesson_details(request, id=None): item = get_object_or_404(Lesson , id = id) #share_string = quote_plus(item.content) initial_data = { "content_type": item.get_content_type, "object_id" : item.id } form = CommentForm(request.POST or None , initial =initial_data) if form.is_valid(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get("object_id") content = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs: parent_obj= parent_qs new_comment, created = Comment.objects.get_or_create( user = request.user, content_type= content_type, object_id = obj_id, content = content, parent = parent_obj ) return HttpResponseRedirect(new_comment.content_object.get_absulte_url()) comments = item.comment context = { "item" : item, "comments" :comments, "form" : form #"share_string" : share_string } return render (request, "lesson_details.html", context)
def render_post(request, post): # render "raw" newsletters if post.type == Post.TYPE_WEEKLY_DIGEST: return HttpResponse(post.html) # select votes and comments if request.me: comments = Comment.objects_for_user(request.me).filter(post=post).all() is_voted = PostVote.objects.filter(post=post, user=request.me).exists() subscription = PostSubscription.get(request.me, post) else: comments = Comment.visible_objects().filter(post=post).all() is_voted = False subscription = None # order comments comment_order = request.GET.get("comment_order") or "-upvotes" if comment_order in POSSIBLE_COMMENT_ORDERS: comments = comments.order_by( comment_order, "created_at") # additionally sort by time to preserve an order # hide deleted comments for battle (visual junk) if post.type == Post.TYPE_BATTLE: comments = comments.filter(is_deleted=False) context = { "post": post, "comments": comments, "comment_form": CommentForm(), "comment_order": comment_order, "reply_form": ReplyForm(), "is_voted": is_voted, "subscription": subscription, } # TODO: make a proper mapping here in future if post.type == Post.TYPE_BATTLE: context["comment_form"] = BattleCommentForm() try: return render(request, f"posts/show/{post.type}.html", context) except TemplateDoesNotExist: return render(request, "posts/show/post.html", context)
def post_detail(request, slug=None): post = get_object_or_404(Post, slug=slug) if post.draft or post.publication_date > timezone.now().date(): if not request.user.is_staff or not request.user.is_superuser: raise Http404 """ Provide initial data to hidden fields of the form via 'initial' """ form = CommentForm(request.POST or None, initial={ 'content_type': post.get_content_type, 'object_id': post.id, }) if form.is_valid() and request.user.is_authenticated(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get("object_id") content = form.cleaned_data.get("content") parent_object = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_queryset = Comment.objects.filter(id=parent_id) if parent_queryset.exists() and parent_queryset.count() == 1: parent_object = parent_queryset.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content, parent=parent_object, ) """ Reload the same page to clean up the input field """ return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) return render(request, "detail.html", { "post": post, "form": form, })
def post_detail(request, slug=None): instance = get_object_or_404(Post, slug=slug) if instance.draft or instance.published > timezone.now().date(): if not request.user.is_staff or not request.user.is_superuser: raise Http404 initial_data = { "content_type": instance.get_content_type, "object_id": instance.id, } form = CommentForm(request.POST or None, initial=initial_data) if form.is_valid() and request.user.is_authenticated: c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get("object_id") content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) comments = instance.comments context = { "instance": instance, "title": instance.title, "comments": comments, "comment_form": form } return render(request, 'post_detail.html', context)
def post_detail(request, slug=None): instance = get_object_or_404(Post, slug=slug) share_string = quote_plus(instance.content) initial_data = { 'content_type': instance.get_content_type, 'object_id': instance.id } form = CommentForm(request.POST or None, initial=initial_data) if form.is_valid(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get('object_id') content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) comments = instance.comments return render(request, 'post_detail.html', context={ 'post': instance, 'share_string': share_string, 'comments': comments, 'comment_form': form, })
def detail(request, id=None): instance = get_object_or_404(Post, id=id) title = instance.title if instance.draft: if not request.user.is_staff or not request.user.is_superuser: raise Http404 comments = comment.objects.filter_by_instance(instance) initial_data = { "content_type": instance.get_content_type, "object_id": instance.id } _form = CommentForm(request.POST or None, initial=initial_data) if _form.is_valid() and request.user.is_authenticated(): c_type = _form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = _form.cleaned_data.get("object_id") content_data = _form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment = comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return redirect(instance.get_absolute_url()) context = { "instance": instance, "title": title, "comments": comments, "coments_form": _form, } return render(request, "blog/detail.html", context)
def posts_detail(request, slug): obj = get_object_or_404(Post, slug=slug) if obj.draft or obj.publish > timezone.now().date(): if not request.user.is_staff or not request.user.is_superuser: raise Http404 share_string = quote_plus(obj.content) print get_read_time(obj.get_markdown()) comments = Comment.objects.get_by_instance(obj) initial_data = {'content_type': obj.get_content_type, 'object_id': obj.id} comment_form = CommentForm(request.POST or None, initial=initial_data) if comment_form.is_valid(): c_type = comment_form.cleaned_data.get('content_type') content_type = ContentType.objects.get(model=c_type) obj_id = comment_form.cleaned_data.get('object_id') content_data = comment_form.cleaned_data.get('content') parent_obj = None try: parent_id = int(request.POST.get('parent_id')) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) #comments = obj.comments context = { 'title': 'DETAIL', 'object': obj, 'share_string': share_string, 'comments': comments, 'comment_form': comment_form } return render(request, 'post_detail.html', context)
def post(self, request, *args, **kwargs): instance = self.get_object() form = CommentForm(self.request.POST or None) if form.is_valid() and request.user.is_authenticated(): data = {} c_type = form.cleaned_data.get("content_type") parent_id = request.POST.get('parent_id') data['content_type'] = ContentType.objects.get(model=c_type) data['object_id'] = form.cleaned_data.get("object_id") data['content'] = form.cleaned_data.get("content") data['user'] = request.user if parent_id: data['parent'] = Comment.objects.get(pk=parent_id) new_comment = Comment(**data) new_comment.save() return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) messages.error(request, "Something went wrong") return HttpResponseRedirect(instance.get_absolute_url())