def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistic_once_read(request, blog) blog_content_type = ContentType.objects.get_for_model(blog) comments = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk, parent=None) # parent=None表示只显示一级评论 context = {} context['previous_blog'] = Blog.objects.filter( created_time__gt=blog.created_time).last() # 取创建时间比当前博客大的博客列表中的最后一条 context['next_blog'] = Blog.objects.filter( created_time__lt=blog.created_time).first( ) # 取创建时间比当前博客小的博客列表中的第一条|可以将first()替换为[0]进行切片 context['blog'] = blog context['comments'] = comments.order_by('-comment_time') context['comment_form'] = CommentForm( initial={ 'content_type': blog_content_type.model, 'object_id': blog_pk, 'reply_comment_id': 0 }) # 对forms表单中的类进行初始化定义值,方便前端代码调用,initial对应的是一个list; blog_content_type是一个对象,blog_content_type.model是这个对象名称字符串 response = render(request, 'blog/blog_detail.html', context) # 响应 response.set_cookie(read_cookie_key, 'true') # 阅读cookie标记 return response
def blog_detail(request,blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistic_once_read(request, blog) context = {} context['previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last() # 取创建时间比当前博客大的博客列表中的最后一条 context['next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first() # 取创建时间比当前博客小的博客列表中的第一条|可以将first()替换为[0]进行切片 context['blog'] = blog response = render(request, 'blog/blog_detail.html',context) # 响应 response.set_cookie(read_cookie_key, 'true') # 阅读cookie标记 return response
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistic_once_read(request, blog) context = {} blog = get_object_or_404(Blog, pk=blog_pk) context['previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first() context['blog'] = blog response = render_to_response('blog/blog_detail.html', context) response.set_cookie(read_cookie_key, 'true') return response
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) key = read_statistic_once_read(request, blog) context = {} next_blog = Blog.objects.filter(created_time__gt=blog.created_time).last() previous_blog = Blog.objects.filter( created_time__lt=blog.created_time).first() context['blog'] = blog context['previous_blog'] = previous_blog context['next_blog'] = next_blog response = render(request, 'blog/blog_detail.html', context) response.set_cookie(key, 'ture', max_age=60) return response
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) key = read_statistic_once_read(request, blog) blog_content_type = ContentType.objects.get_for_model(blog) context = {} context['previous_blog'] = Blog.objects.filter( created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter( created_time__lt=blog.created_time).first() context['blog'] = blog response = render(request, 'blog/blog_detail.html', context) response.set_cookie(key, 'true') return response
def blog_detail(request, blog_id): blog = get_object_or_404(Blog, id=blog_id) # 获取key read_cookie_key = read_statistic_once_read(request, blog) context = { 'pre_blog': Blog.objects.filter(created_time__gt=blog.created_time).last(), 'blog': get_object_or_404(Blog, id=blog_id), 'next_blog': Blog.objects.filter(created_time__lt=blog.created_time).first(), 'login_form': LoginForm(), } response = render(request, 'blog_detail.html', context) response.set_cookie(read_cookie_key, 'true') # 阅读标记 return response