Пример #1
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog,pk= blog_pk)
    read_cookie_key = read_statistics_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()    #同上,获取下一条博客内容
    context['blog'] = blog
    response = render_to_response('blog/blog_detail.html', context)
    response.set_cookie(read_cookie_key, 'true')#将看过的文章ID存储到cookie中,未设置过期时间,默认为退出浏览器该cookie过期
    return response
Пример #2
0
def blog_detail(request, blog_pk):  # 博客详情
    blog = get_object_or_404(Blog, pk=blog_pk)  # 取出对应的blog_pk中的Blog查询集
    read_cookie_key = read_statistics_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()
    context["blog"] = blog
    context["login_form"] = LoginForm()
    response = render(request, "blog/blog_detail.html", context)
    response.set_cookie(read_cookie_key, "true")
    return response
Пример #3
0
def blog_detail(request, blog_pk):
    # 由前端提供的数据最好使用get_object_or_404
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_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()
    context['blog'] = blog
    # context['login_form'] = LoginForm()

    response = render(request, 'blog/blog_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key.format(blog_pk), 'true')  # 阅读cookie标记
    return response
Пример #4
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_once_read(request, blog)

    context = {}
    context['previou_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(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #5
0
def blog_detail(request, blog_pk):  # 博客详情
    context = {}
    blog = get_object_or_404(Blog, pk=blog_pk)  # 可能请求主键值没有对应的博客那么就返回404
    read_cookie_key = read_statistics_once_read(request, blog)
    context['blog'] = blog
    context['pre_blog'] = Blog.objects.filter(
        create_time__gt=blog.create_time).last()
    '''
    筛选博客创建时间大于当前博客创建时间的博客里的最后一条
    '''
    context['next_blog'] = Blog.objects.filter(
        create_time__lt=blog.create_time).first()
    response = render(request, 'blog/blog_detail.html', context)
    response.set_cookie(read_cookie_key, 'true')
    return response
Пример #6
0
def blog_detail(request, blog_id):
    blog = get_object_or_404(Blog, pk=blog_id)
    read_cookie_key = read_statistics_once_read(request, blog)

    context = {}
    context["blog"] = blog
    context['user'] = request.user
    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()
    response = render(request, "blog/blog_detail.html", context)
    # cookie的有效期
    response.set_cookie(read_cookie_key, 'true')
    return response
Пример #7
0
def blog_detail(request, blog_pk):
    blog = Blog.objects.get(pk=blog_pk)
    read_cookie = read_statistics_once_read(request, blog)
    previous_blog = Blog.objects.filter(
        created_time__gt=blog.created_time).last()
    next_blog = Blog.objects.filter(created_time__lt=blog.created_time).first()

    context = {
        'blog': blog,
        'previous_blog': previous_blog,
        'next_blog': next_blog,
    }
    response = render(request, 'blog/blog_detail.html', context)  # 响应
    response.set_cookie(read_cookie, 'true')  # 阅读cookie标记
    return response
Пример #8
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_once_read(request, blog)  # 获取cookie标记

    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=context)  # 响应
    response.set_cookie(
        read_cookie_key,
        'true')  # 阅读cookie标记  # 设置cookie,是字典,比如第10篇是{'blog_10_readed':true}
    return response
Пример #9
0
def blog_detail(request, blog_pk):  #显示具体的blog界面
    blog = get_object_or_404(Blog, pk=blog_pk)  #pk是主键
    read_cookie_key = read_statistics_once_read(request, blog)

    context = {}
    context['previous_blog'] = Blog.objects.filter(
        created_time__gt=blog.created_time).last(
        )  #当前打开博客时间的前一天;__gt是两个英文的下划线,少了出错
    context['next_blog'] = Blog.objects.filter(
        created_time__lt=blog.created_time).first()  #当前打开博客时间的后一天
    context['blog'] = blog  #当前打开的博客_p

    response = render(request, 'blog/blog_detail.html', context)
    response.set_cookie(read_cookie_key, 'true')  #阅读cookie标记
    return response
Пример #10
0
def blog_detail(req, blog_id):
	blog = get_object_or_404(Blog, id=blog_id)
	blog.content = markdown.markdown(
		blog.content, extensions=[
			'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc'])
	read_cookie_key = read_statistics_once_read(req, blog)
	
	context = dict()
	context['blog'] = blog
	context['blog_tags'] = blog.tag.all()
	context['pre_blog'] = Blog.published.filter(publish__gt=blog.publish).last()
	context['next_blog'] = Blog.published.filter(publish__lt=blog.publish).first()
	response = render(req, 'blog/blog_detail.html', context)
	response.set_cookie(read_cookie_key, 'true')
	return response
Пример #11
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_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)

    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
    context['comments'] = comments
    context['comment_form'] = CommentForm(initial={'content_type': blog_content_type.model, 'object_id': blog_pk})
    response = render(request, 'blog/blog_detail.html', context) # 响应
    response.set_cookie(read_cookie_key, 'true') # 阅读cookie标记
    return response
Пример #12
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_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()  #同上,获取下一条博客内容
    context['blog'] = blog
    response = render_to_response('blog/blog_detail.html', context)
    response.set_cookie(read_cookie_key,
                        'true')  #将看过的文章ID存储到cookie中,未设置过期时间,默认为退出浏览器该cookie过期
    return response
Пример #13
0
def barter_detail(request, barter_pk):
    barter = get_object_or_404(Barter, pk=barter_pk)
    read_cookie_key = read_statistics_once_read(request, barter)
    mybarters = Barter.objects.filter(status='1', seller_id=request.user.id)

    context = {}
    context['previous_barter'] = Barter.objects.filter(
        created_time__gt=barter.created_time).last()
    context['next_barter'] = Barter.objects.filter(
        created_time__lt=barter.created_time).first()
    context['barter'] = barter
    context['mybarters'] = mybarters
    response = render(request, 'barter/barter_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #14
0
def blog_detail(request, blog_pk):
    blog = Blog.objects.get(pk=blog_pk)
    read_cookie_key = read_statistics_once_read(request, blog)
    c_time = blog.created_time
    previous_blog = Blog.objects.filter(created_time__gt=c_time).last()
    next_blog = Blog.objects.filter(created_time__lt=c_time).first()

    content = {
        'previous_blog': previous_blog,
        'next_blog': next_blog,
    }
    content['blog'] = get_object_or_404(Blog, pk=blog_pk)
    response = render_to_response('blog/blog_detail.html', content)
    response.set_cookie(read_cookie_key, 'true')
    return response
Пример #15
0
def article_detail(request, b_id):

    article = get_object_or_404(blogv1, pk=b_id)
    read_cookie_key = read_statistics_once_read(request, article)

    context = {}
    context['article_obj'] = article
    context['previous_blog'] = blogv1.objects.filter(
        created_time__gt=article.created_time).last()
    context['next_blog'] = blogv1.objects.filter(
        created_time__lt=article.created_time).first()

    response = render(request, 'blog/article_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #16
0
def blog_detail(request, blog_pk):
    # 计数
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_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)
    # 判断书否有缓存
    '''
    if not request.COOKIES.get('blog_%s_readed' % blog_pk):
        # blog.readed_num += 1
        # blog.save()
        
        ct = ContentType.objects.get_for_model(Blog)

        if ReadNum.objects.filter(content_type=ct, object_id=blog.pk).count():
            # 存在对应的记录
            readnum = ReadNum.objects.get(content_type=ct, object_id=blog.pk)
        else:
            # 不存在对应的记录
            readnum = ReadNum(content_type=ct, object_id=blog.pk)

        # 计数加1并保存
        readnum.read_num += 1
        readnum.save()
        '''

        
    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
    # context['login_form'] = LoginForm() # 公用了
    # context['comment_count'] = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk).count()
    # 应为模板包去掉
    # context['comments'] = comments.order_by('-comment_time')
    # date = {}
    # data['content_typs'] = blog_content_type.model
    # data['object_id'] = blog_pk
    # context['comment_form'] = CommentForm(initial=data)
    # 因为引用模板包重复定义所以将此注释
    # context['comment_form'] = CommentForm(initial={'content_type':blog_content_type.model, 'object_id':blog_pk, 'reply_comment_id': 0})
    response = render(request, 'blog/blog_detail.html', context) # 响应
    # 阅读cookie的标记
    response.set_cookie(read_cookie_key, 'true') # 默认的清除cookie的时候是关闭浏览器的时候
    # response.set_cookie('blog_%s_readed' % blog_pk, 'true', max_age=60)
    return response
Пример #17
0
def blog_detail(request, art_id):  # 博客id
    context = {}
    try:
        blog = get_object_or_404(Blog, pk=art_id)
        read_cookie_key = read_statistics_once_read(request, blog)
    except Blog.DoesNotExist:
        raise Http404('not exist')
    context['next_blog'] = Blog.objects.filter(
        create_time__gt=blog.create_time).last()  # 下一篇博客,通过创建时间而排序的
    context['previous_blog'] = Blog.objects.filter(
        create_time__lt=blog.create_time).first()  # 上一篇博客
    context['blog'] = blog
    context['login_form'] = LoginForm()
    response = render(request, 'blog_detail.html', context)  # 返回响应
    response.set_cookie(read_cookie_key, 'true')  # 设置cookie
    return response
Пример #18
0
def blog_detail(request,blog_pk):
    blog = get_object_or_404(Blog,pk=blog_pk)
    read_cookie_key = read_statistics_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)

    context = {}
    context['previous_blog'] = Blog.objects.filter(create_time__gt=blog.create_time).last()
    context['next_blog'] = Blog.objects.filter(create_time__lt=blog.create_time).first()
    context['blog'] = blog
    # context['login_form'] = LoginForm()
    # context['comments'] = comments.order_by('-comment_time')
    context['user'] = request.user
    response = render(request,'blog/blog_detail.html',context) #相应
    response.set_cookie(read_cookie_key,'true')  #阅读cookile标记
    return response
Пример #19
0
 def get(self, request, blog_pk):
     context = {}
     blog = get_object_or_404(Blog, pk=blog_pk)
     blog.content = markdown.markdown(blog.content,
                                      extensions=[
                                          'markdown.extensions.extra',
                                          'markdown.extensions.codehilite',
                                          'markdown.extensions.toc',
                                      ])
     context['blog'] = blog
     context['previous_blog'] = Blog.objects.filter(id__lt=blog_pk).first()
     context['next_blog'] = Blog.objects.filter(id__gt=blog_pk).last()
     response = render(request, 'blog_detail.html', context)
     key = read_statistics_once_read(request, blog)
     response.set_cookie(key, 'true')
     return response
Пример #20
0
def msg_detail(request, msg_pk):
    msg = get_object_or_404(Msg, pk=msg_pk)
    read_cookie_key = read_statistics_once_read(request, msg)

    context = {}
    # 去当前打开的这篇数据并找到时间进行比较,取到上一条数据
    context['previous_msg'] = Msg.objects.filter(
                                created_time__gt=msg.created_time).last()
    # 下一条数据
    context['next_msg'] = Msg.objects.filter(created_time__lt=msg.created_time).first()
    # 当前数据
    context['msg'] = msg

    response = render(request, 'msg/msg_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记,告诉浏览器保存信息 类似字典
    return response
Пример #21
0
def blog_detail(request, article_id):
    content = get_object_or_404(models.Blog, id=int(article_id))
    read_cookie_key = read_statistics_once_read(request, content)

    # 热搜数据的查询
    blog_content_type = ContentType.objects.get_for_model(models.Blog)

    # 今日热搜数据
    today_hot_data = get_today_hot_data(blog_content_type)

    # 相关评论类容
    comments = Comment.objects.filter(content_type=blog_content_type, object_id=content.pk, parent=None)

    response = render(request, 'article/article.html', locals())
    response.set_cookie(read_cookie_key, 'true')
    return response
Пример #22
0
def blog_detail(request, blog_pk):

    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_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()  # 获取下一篇博客
    context['blog'] = blog
    response = render(request, 'blog/blog_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key,
                        'true')  # 键值  权值   max_age有效期持续时间   expires 有效期到多久为止,
    #有了expires 则 max_age 无效 如果两个都不设置  那么打开浏览器时一直有效 关闭浏览器失效
    return response
Пример #23
0
def blog_detail(requests, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    obj_key = read_statistics_once_read(requests, blog)

    context = {
        'blog':
        blog,
        'previous_blog':
        Blog.objects.filter(created_time__gt=blog.created_time).last(),
        'next_blog':
        Blog.objects.filter(created_time__lt=blog.created_time).first(),
    }
    response = render(requests, 'blog/blog_datail.html', context)
    response.set_cookie(obj_key, 'true')

    return response
Пример #24
0
def blog_detail(request, blog_pk):
    context = {}
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_once_read(
        request, blog)  # 调用方法为博客阅读计数,得到此博客的阅读cookie
    context["blog"] = blog
    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_dates"] = Blog.objects.dates('created_time',
                                               'day',
                                               order='DESC')
    response = render_to_response("blog/blog_detail.html", context)
    response.set_cookie(read_cookie_key, "true")
    return response
Пример #25
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    created_time = blog.created_time
    read_cookie_key = read_statistics_once_read(request, blog)

    context = {}
    previous_blog = Blog.objects.filter(created_time__gt=created_time).last()
    next_blog = Blog.objects.filter(created_time__lt=created_time).first()
    context['blog'] = blog
    context['previous_blog'] = previous_blog
    context['next_blog'] = next_blog
    context['user'] = request.user  #获取用户信息
    response = render(request, 'blog/blog_detail.html', context)  #相应
    response.set_cookie(read_cookie_key, 'true',
                        max_age=60)  #max_age,expires取一个用
    return response
Пример #26
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    read_cookie_key = read_statistics_once_read(request, blog)
    blog_content = customer_markdown(blog.content)
    # blog_content = blog.content
    context = {}
    context['next_blog'] = Blog.objects.filter(
        created_time__gt=blog.created_time).last()
    context['previous_blog'] = Blog.objects.filter(
        created_time__lt=blog.created_time).first()
    context['blog'] = blog
    context['blog_content'] = blog_content
    context['login_form'] = LoginForm()
    response = render(request, 'blog/blog_detail.html', context)  # 响应
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #27
0
def blog_detail(request, blog_pk):
    # blog_content = Blog.objects.get(pk=blog_id)
    blog_content = get_object_or_404(Blog, pk=blog_pk)  # 通过博客id,读取
    # # 判断cookie是否存在
    # if not request.COOKIES.get('blog_%s_readed' % blog_pk):
    #     if ReadNum.objects.filter(blog=blog_content).count() != 0:
    #         # 存在记录 查找该博客
    #         readnum = ReadNum.objects.filter(blog=blog_content)
    #     else:
    #         # 不存在对应记录 创建该博客计数
    #         readnum = ReadNum(blog=blog_content)
    #     # 计数加1
    #     readnum.read_num += 1
    #     readnum.save()

    # if not request.COOKIES.get('blog_%s_readed' % blog_pk):
    #     ct = ContentType.objects.get_for_model(Blog)
    #     if ReadNum.objects.filter(content_type=ct, object_id=blog_content.pk).count() != 0:  # 查找该博客是否存在
    #         readnum = ReadNum.objects.get(content_type=ct, object_id=blog_content.pk)  # 存在 获取该模型实例
    #     else:
    #         readnum = ReadNum(content_type=ct, object_id=blog_content.pk)  # 不存在 实例化该模型
    #     readnum.read_num += 1  # 计数
    #     readnum.save()  # 保存
    read_cookie_key = read_statistics_once_read(request,
                                                blog_content)  # 通过调用封装好的函数

    blog_content_type = ContentType.objects.get_for_model(blog_content)
    comments = Comment.objects.filter(content_type=blog_content_type,
                                      object_id=blog_content.pk)

    context = {}
    context['blog'] = blog_content
    # filter 筛选条件 id__in=[1, 3] id在1或3里
    # 上一篇博客 比当前博客时间大的最后一条
    context['previous_blog'] = Blog.objects.filter(
        create_time__gt=blog_content.create_time).last()
    # 下一篇博客 [0]方式会导致第一篇indexerror
    context['next_blog'] = Blog.objects.filter(
        create_time__lt=blog_content.create_time).first()
    context['user'] = request.user  # 获取当前登录用户
    context['comments'] = comments

    repos = render(request, 'blog/blog_detail.html', context)
    # 设置cookie
    repos.set_cookie(read_cookie_key,
                     'true')  # 参数expires指定时间过期  参数max_age多长时间后过期(秒)
    return repos
Пример #28
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    blog.content = markdown.markdown(blog.content, 
                                    extensions=[
                                        'markdown.extensions.extra',
                                        'markdown.extensions.codehilite',
                                        'markdown.extensions.toc',
                                    ])
    read_cookie_key = read_statistics_once_read(request, blog)
    
    context = {}
    context['blog'] = blog
    context['previous_blog'] = Blog.objects.filter(create_time__gt=blog.create_time).last()
    context['next_blog'] = Blog.objects.filter(create_time__lt=blog.create_time).first()
    response = render(request, 'blog/blog_detail.html', context)    # 相应
    response.set_cookie(read_cookie_key, 'true')    # 阅读cookie标记
    return response
Пример #29
0
def detail(request, pk):
    """显示当前选择文章内容"""
    # 接收了一个pk值,这个值是在url中传递的主键,利用该主键可以找到文章的对象
    # get_object_or_404的用法是(模型名,get方法)
    post = get_object_or_404(Post, pk=pk)
    post_all_list = Post.objects.all()
    # 使用公共的get_blog_list_common_data的方法
    context = get_blog_list_common_data(request, post_all_list)

    # read_statistics_once_read是在read_statistics应用中的方法,表示计数+1
    read_cookie_key = read_statistics_once_read(request, post)

    post_content_type = ContentType.objects.get_for_model(Post)
    # comments = Comment.objects.filter(content_type=post_content_type, object_id=post.pk, parent=None)

    # 在django中不能使用>=或<=,所以django自定义了__gt和__lt
    # 目的:得出创建时间比当前博客创建时间较晚的所有博客列表的最后一篇博客,也就是当前博客的上一篇
    # 因为博客是按照创建时间的先后来排序的:即先创建的靠后,那么上一篇博客创建时间晚于当前博客
    previous_post = Post.objects.filter(
        created_time__gt=post.created_time).last()

    # 目的:得出创建时间比当前博客创建时间较早的所有博客列表的第一篇博客,也就是当前博客的下一篇
    # 因为博客是按照创建时间的先后来排序的:即先创建的靠后,那么上一篇博客创建时间早于当前博客
    next_post = Post.objects.filter(created_time__lt=post.created_time).first()

    context.update({
        'article': post.body,
        'title': post.title,
        'author': post.author,
        'created_time': post.created_time,
        'category': post.category,
        'previous_post': previous_post,
        'next_post': next_post,
        'read_num': post.get_read_num,
        'user': request.user,
        'post_id': post.id,
        'post': post,
        'login_form': LoginForm(),
        # 'comments': comments.order_by('-comment_time'),
        # 'comment_form': CommentForm(initial={'content_type': post_content_type.model, 'object_id': pk, 'reply_comment_id': 0}),
        # 'comment_count':Comment.objects.filter(content_type=post_content_type, object_id=post.pk).count()
    })
    response = render(request, 'blog/detail.html', context)
    # 第一个参数是键,键值,和过期时间
    response.set_cookie(read_cookie_key, 'true')  # 阅读cookie标记
    return response
Пример #30
0
def blog_detail(request, blog_pk):
    """博客详情页"""
    blog = get_object_or_404(Blog, pk=blog_pk)  # 根据传入blog_pk的ID来找到具体对应博客文章

    read_cookie_key = read_statistics_once_read(request, blog)

    pre_blog = Blog.objects.filter(id__gt=blog.id).last()  # 博客的上一篇文章
    next_blog = Blog.objects.filter(id__lt=blog.id).first()  # 博客的下一篇文章
    response = render(request, 'blog/blog_detail.html', {
        'blog': blog,
        'pre_blog': pre_blog,
        'next_blog': next_blog
    })  #响应

    # 设置cookie,打开过这篇文章即写入cookie,key是'blog_%s_readed' % blog_pk,value是'true'
    response.set_cookie(read_cookie_key, 'true')  #阅读cookie的标记
    return response
Пример #31
0
def blog_detail(request, blog_pk):
    blog = get_object_or_404(Blog, pk=blog_pk)
    key = read_statistics_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)
    context = {}
    context['blog'] = get_object_or_404(Blog, id=blog_pk)
    context['previous_blog'] = Blog.objects.filter(
        created_time__gt=context['blog'].created_time).last()  #or [-1]
    context['next_blog'] = Blog.objects.filter(
        created_time__lt=context['blog'].created_time).first()  #or [0]
    context['user'] = request.user
    context['comments'] = comments
    response = render(request, 'blog/blog_detail.html', context)  #响应
    response.set_cookie(key, 'true')  #max_age=60, expires=datetime
    return response