Vue全栈开发旅游网项目(6)-接口开发
1.景点详情接口开发
1.设计响应数据结构
文件地址:sight/serializers.py
创建类:
class SightDetailSerializers(BaseSerializer):#景点详情def to_dict(self):obj = self.objreturn {'id': obj.id,'name': obj.name,'desc': obj.desc,'img': obj.banner_img.url,'content': obj.content,'score': obj.score,'min_price': obj.min_price,'province': obj.province,'city': obj.city,'area': obj.area,'town': obj.town,'comment_count': 0}
2.设计视图函数
向数据库中获取数据,文件地址:sight/views.py。
class SightDetailView(DetailView):#景点详情信息def get_queryset(self):# 获取数据return Sight.objects.all()def render_to_response(self, context, **response_kwargs):#响应数据page_obj = context['object']if page_obj is not None:if page_obj.is_valid ==False:return NotFoundJsonResponse()date = serializers.SightDetailSerializers(page_obj).to_dict()return http.JsonResponse(date)return NotFoundJsonResponse()
3.构建地址的函数
文件地址:sight/urls.py
访问成功可执行sight/views.py
配置具体函数地址,参数1:自定义访问地址,参数2:访问函数的位置,参数3:连接名
urlpatterns = [path('sight/detail/<int:pk>',views.SightDetailView.as_view(),name='sight_detail'),
]
2.景点评论接口开发
1.设计返回图片格式
创建文件system/serializers.py
序列化基础图片,其他列表需要关联图片时,引用该类
from utils.serializers import BaseSerializer
class BaseImageSerializer(BaseSerializer):def to_dict(self):image = self.objreturn {'img': image.img.url,'summary': image.summary,}
2.评论列表序列化
文件地址:sight/serializers.py
class CommentListSerializers(BaseListPageSerializer):#评论列表def get_object(self, obj):user = obj.userimages = []for image in obj.images.filter(is_valid = True):images.append({'img': image.img.url,'summary': image.summary})return {'user': {'pk': user.pk,'nickname': user.nickname},'pk': obj.pk,'content': obj.content,'is_top': obj.is_top,'love_count': obj.love_count,'score': obj.score,'is_public': obj.is_public,'images': images,'created_at': obj.created_at.strftime('%Y-%m-%d %H:%M:%S'),}
3.设计视图函数
文件地址:sight/views.py
class SightCommentListView(ListView):#景点评论列表paginate_by = 10def get_queryset(self):#根据id查询景点信息sight_id = self.kwargs.get('pk', None)sight = Sight.objects.filter(pk = sight_id,is_valid = True).first()if sight:return sight.comments.filter(is_valid = True)return Comment.objects.none()def render_to_response(self, context, **response_kwargs):page_obj = context['page_obj']if page_obj is not None:data = serializers.CommentListSerializers(page_obj).to_dict()return http.JsonResponse(data)return NotFoundJsonResponse
4.构建地址函数
文件地址:sight/urls.py
path('comment/list/<int:pk>',views.SightCommentListView.as_view(),name='sight_comment_list')
3.景点信息及门票接口开发
1.设计响应数据结构
创建门票和景点列表的类
class TicketListSerializers(BaseListPageSerializer):#门票列表def get_object(self, obj):return {'pk': obj.pk,'name': obj.name,'desc': obj.desc,'types': obj.types,'price': obj.price,'discount': obj.discount,'total_stock': obj.total_stock,'remain_stock': obj.remain_stock}
class SightInfoSerializer(BaseSerializer):#景点列表def to_dict(self):obj = self.objreturn {'pk': obj.sight.pk,'entry_explain': obj.entry_explain,'play_way': obj.play_way,'tips': obj.tips,'traffic': obj.traffic}
2.设计视图函数
class SightTicketListView(ListView):#景点门票列表视图paginate_by = 10def get_queryset(self):#根据景点id查询sight_id = self.kwargs.get('pk', None)return Ticket.objects.filter(sight_id = sight_id,is_valid = True)def render_to_response(self, context, **response_kwargs):page_obj = context['page_obj']#获取对象if page_obj is not None:data = serializers.TicketListSerializers(page_obj).to_dict()return http.JsonResponse(data)return NotFoundJsonResponse()class SightInfoDetail(DetailView):#当地址pk为None时,以下配置会生效pk_url_kwarg = None#URL中参数的名称slug_url_kwarg = 'pk'#URL中pk对应的哪一个字段(数据库中的字段)slug_field = 'sight__pk'#景点详情列表视图def get_queryset(self):return Info.objects.all()def render_to_response(self, context, **response_kwargs):page_obj = context['object']if page_obj is not None:data = serializers.SightInfoSerializer(page_obj).to_dict()return http.JsonResponse(data)return NotFoundJsonResponse()
3.构建地址函数
urlpatterns = [path('ticket/list/<int:pk>/',views.SightTicketListView.as_view(),name='sight_ticket_list'),path('sight/info/<int:pk>/',views.SightInfoDetailView.as_view(),name='sight_info')
]
4.网页检测网址
http://localhost:8000/sight/sight/detail/1/
http://localhost:8000/sight/comment/list/1/
http://localhost:8000/sight/ticket/list/1/
http://localhost:8000/sight/sight/info/1/