20Django跑通文章详情页

文章详情页和文章列表页一样,都需要组装数据,因此我们要先编写一个组装数据的方法(组装数据的格式文档已经规定好):

因为客户留言暂时不做,所以可以把留言messages和留言总数message_count这两个字段写个空值,又因为上一篇下一篇暂时页做不了,所以我们先给个假值,最后我们拼装的数据应该是这个样子的(目的是先把业务跑起来):

def make_topic_res(self,author,author_topic):
res = {'code':200,'data':{}}
res['data']['nickname'] = author.nickname
res['data']['title'] = author_topic.title
res['data']['category'] = author_topic.category
res['data']['created_time'] = author_topic.create_time.strftime('%Y-%m-%d %H:%M:%S')
res['data']['content'] = author_topic.content
res['data']['introduce'] = author_topic.introduce
res['data']['author'] = author.username
res['data']['last_id'] = None
res['data']['last_title'] = ''
res['data']['next_id'] = None
res['data']['next_title'] = ''
res['data']['messages'] = []
res['data']['messages_count'] = 0
return res

1分析获取用户具体博客内容接口:

URL:http://127.0.0.1:8000/v1/topics/< username>?t_id=1111

我们前面做的获取博客列表页也走的上面的地址,也是get请求,并且也有查询字符串是category

而本次文章详情页接口请求地址和文章列表地址前缀是一样的,区别只是后面的查询串不一样,因此我们可以在原视图类中进行业务拆分

拆分逻辑如下:

如果请求地址中有t_id这个字符串,那么就是获取文章详情页,如果没有t_id这个字段,那么他就是获取文章列表页数据

3分析原路由内的视图函数:

该视图函数主要分为上下两个部分,上部分主要是确定访客身份的,下半部分是判断查询字符串catrgory的值,也就是说,我们可以在这两段逻辑之间添加一个判断t_id字符串的逻辑

 #查看文章
    def get(self,request,username):
        #访问博客的人分类两类,一类是访客,另外一类是博主本人,先判断传过来的用户名在数据库中是否存在
        #确认博主身份
        try:
            author = UserProfile.objects.get(username=username)
            #dict_author = model_to_dict(author)
            #dict_author['avatar'] = author.avatar.url
        except Exception as e:
            result = {'code':301,'error':'作者不存在'}
            return JsonResponse(result)
        #判断访客身份
        visitor = get_user_by_request(request)

        visitor_username = None
        if visitor:
            visitor_username = visitor.username
        print('--以上代码基本都是确定访客身份的--')

#根据获取前端的字符串拆分业务
        t_id = request.GET.get('t_id')
        print(t_id)
        if t_id:
            #获取指定的文章数据
            t_id = int(t_id)
            #如果是博主,就从数据库里查出博主的t_id,否则你只能拿到博主公开的t_id
            if visitor_username == username:
                try:
                    author_topic = Topic.objects.get(id=t_id,author_id=username) #注意这里是id和author_id是and关系,隐私权限重点注意
                except Exception as e:
                    result = {'code':20211217,'error':'博主身份异常'}
                    return JsonResponse(result)
            else:
                try:
                    author_topic = Topic.objects.get(id=t_id,author_id=username,limit='public')
                except Exception as e:
                    result = {'code':302,'error':'当前文章不存在或者你查询的字符串属于作者隐私'}
                    return JsonResponse(result)
            #响应前端数据
            res = self.make_topic_res(author,author_topic)
            print(res)
            return JsonResponse(res)

        else:
            print('--以下代码基本就是获取具体查询字符串条件--')

            #温柔取值,没有返回None
            category = request.GET.get('category')
            #判断是否查分类了
            if category in ['python','linux']:

                #如果访客的用户名等于当前博主的用户名意味着博主再访问自己的博客
                if visitor_username == username:
                    author_topics = Topic.objects.filter(author_id=username,category=category)
                else:
                    #否则的话只能看公开的
                    author_topics = Topic.objects.filter(author_id=username,limit='public',category=category)
            else:
                if visitor_username == username:
                    author_topics = Topic.objects.filter(author_id=username)
                else:
                    #否则的话只能看公开的
                    author_topics = Topic.objects.filter(author_id=username,limit='public')
            #把博主的和查到的文章作为参数都传到响应方法make_topics_res里
            #return JsonResponse({'author':dict_author,'list_arctile':[model_to_dict(i) for i in author_topics]})
            res = self.make_topics_res(author, author_topics)
            print(res)
            return JsonResponse(res)

 

上一篇:20Django跑通文章详情页


下一篇:Go: Methods and interfaces