是否有理由个性化基于 Django 类的视图?

问题描述 投票:0回答:1

在 Django 教程中,我看到有人写道:

class GetUserProfileView(APIView):
     def get(self, request, format=None):
          # user profile get code

class UpdateProfileView(APIView):
     def put(self, request, format=None):
          # user profile put code

而对我来说,一个初学者来说,以一个视图来组织它似乎更有意义:

class ProfileView(APIView):
     def get(self, request, format=None):
          # user profile get code

     def put(self, request, format=None):
          # user profile put code

将它们组织到不同的视图中是否有原因,或者教程制作者缺乏经验?

python django django-views
1个回答
0
投票

通常,每个开发人员都有自己的编写和组织代码的风格。

就您而言,遵守诸如DRYKISS之类的干净代码原则,我建议使用这种风格:

class UserProfileView(APIView):
     def get(self, request, format=None):
          # user profile get code

     def put(self, request, format=None):
          # user profile put code

这消除了基于类的视图的重复和冗余,并使您的代码更清晰。 (我总是使用它;)

© www.soinside.com 2019 - 2024. All rights reserved.