def post_comment(request, post_pk): post = get_object_or_404(Post, pk=post_pk) # check the method if request.method == 'POST': print('receive the post') # construct the instance of comment form form = CommentForm(request.POST) # check if the data in the form is correct in format if form.is_valid(): print('valid') comment = form.save( commit=False) # generate the comment from the comment form comment.post = post # relate to the post comment.save() # save to comment database return redirect(post) # redirect to post.get_absolute_url else: # find invalid, return to error page comment_list = post.comment_set.all() context = { 'post': post, 'form': form, 'comment_list': comment_list } return render(request, 'iblog/detail.html', context=context) else: return redirect(post)
def post_detail(request, slug=None): instance = get_object_or_404(Post, slug=slug) if instance.draft: if not request.user.is_staff or not request.user.is_superuser: raise Http404 #content_type = ContentType.objects.get_for_model(Post) #object_id = instance.id #comments = Comment.objects.filter(content_type=content_type,object_id=object_id) #comments = Comment.objects.filter_by_instance(instance) #print(get_read_time(instance.get_markdown())) initial_data = { "content_type": instance.get_content_type, "object_id": instance.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) object_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 is not None: 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=object_id, content=content_data, parent=parent_obj) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) comments = instance.comments context = { 'qs': instance, 'comments': comments, "comment_form": comment_form, 'url': '/posts/' } return render(request, 'post_detail.html', context)
def read(request, id=None): item = get_object_or_404(Post, id=id) if item.publish > timezone.now().date() or item.draf: if not request.user.is_staff or not request.user.is_superuser: raise Http404 share_string = quote_plus(item.content) #print(get_read_time(item.get_markdown())) ## next two field used without modelManager #content_type = ContentType.objects.get_for_model(Post) #obj_id = item.id 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_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(): 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 = Comment.objects.filter_by_instance(item) comments = item.comments context = { "item": item, "share_string": share_string, "comments": comments, "comment_form": form, } return render(request, 'read.html', context)
def comment(request, post_pk): # 先获取被评论的文章,因为后面需要把评论和被评论的文章关联起来。 # 这里我们使用了 django 提供的一个快捷函数 get_object_or_404, # 这个函数的作用是当获取的文章(Post)存在时,则获取;否则返回 404 页面给用户。 post = get_object_or_404(Post, pk=post_pk) # django 将用户提交的数据封装在 request.POST 中,这是一个类字典对象。 # 我们利用这些数据构造了 CommentForm 的实例,这样就生成了一个绑定了用户提交数据的表单。 form = CommentForm(request.POST) # 当调用 form.is_valid() 方法时,django 自动帮我们检查表单的数据是否符合格式要求。 if form.is_valid(): # 检查到数据是合法的,调用表单的 save 方法保存数据到数据库, # commit=False 的作用是仅仅利用表单的数据生成 Comment 模型类的实例,但还不保存评论数据到数据库。 comment = form.save(commit=False) # 将评论和被评论的文章关联起来。 comment.post = post # 最终将评论数据保存进数据库,调用模型实例的 save 方法 comment.save() # 这里导入 django 的 messages 模块,使用 add_message 方法增加了一条消息, # 消息的第一个参数是当前请求,因为当前请求携带用户的 cookie,django 默认将详细存储在用户的 cookie 中。 # 第二个参数是消息级别,评论发表成功的消息设置为 messages.SUCCESS,这是 django 已经默认定义好的一个整数,消息级别也可以自己定义。 # 紧接着传入消息的内容,最后 extra_tags 给这条消息打上额外的标签,标签值可以在展示消息时使用,比如这里我们会把这个值用在模板中的 HTML 标签的 class 属性,增加样式。 messages.add_message(request, messages.SUCCESS, '评论发表成功!', extra_tags='success') # 重定向到 post 的详情页,实际上当 redirect 函数接收一个模型的实例时,它会调用这个模型实例的 get_absolute_url 方法, # 然后重定向到 get_absolute_url 方法返回的 URL。 return redirect(post) # 检查到数据不合法,我们渲染一个预览页面,用于展示表单的错误。 # 注意这里被评论的文章 post 也传给了模板,因为我们需要根据 post 来生成表单的提交地址。 context = { 'post': post, 'form': form, } messages.add_message(request, messages.ERROR, '评论发表失败!请修改表单中的错误后重新提交。', extra_tags='danger') return render(request, 'comments/preview.html', context=context)
def posts_detail(request, slug=None): instance = get_object_or_404(Post, slug=slug) # content_type = ContentType.objects.get_for_model(Post) # object_id = instance.id # comments = Comment.objects.filter(content_type=content_type, object_id=object_id) initial_data = { 'content_type': instance.get_content_type, 'object_id': instance.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) object_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=object_id, content=content_data, parent=parent_obj) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) comments = instance.comments context = { 'title': 'Detail', 'object': instance, 'comments': comments, 'comment_form': comment_form, 'words_count': count_words(instance.get_markdown()), 'read_time': get_read_time(instance.get_markdown()), } return render(request, 'post_detail.html', context)