我正在学习DRF,而且我对DRF序列化程序如何在browsableapi中生成HTML格式感到困惑。我有UserRegistrationAPIView和UserRegistrationSerializer。
class UserRegistrationAPIView(CreateAPIView):
authentication_classes = ()
permission_classes = ()
serializer_class = UserRegistrationSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
user = serializer.instance
token, created = Token.objects.get_or_create(user=user)
data = serializer.data
data["token"] = token.key
headers = self.get_success_headers(serializer.data)
return Response(data, status=status.HTTP_201_CREATED, headers=headers)
class UserRegistrationSerializer(serializers.ModelSerializer):
# password = serializers.CharField(write_only=True, style={'input_type': 'password'})
confirm_password = serializers.CharField(write_only=True, style={'input_type': 'password'})
confirm_password1 = serializers.CharField(write_only=True, style={'input_type': 'password'})
class Meta:
model = User
fields = ("id", "username", "email", "password", "confirm_password", "date_joined","confirm_password1")
def create(self, validated_data):
del validated_data["confirm_password"]
del validated_data["confirm_password1"]
return super(UserRegistrationSerializer, self).create(validated_data)
def validate(self, attrs):
if attrs.get('password') != attrs.get('confirm_password'):
raise serializers.ValidationError("Those passwords don't match.")
return attrs
当我转到此视图的URL时,我感到很困惑,它会自动生成HTML表单字段。因此,这是否意味着它使用序列化程序来生成HTML字段。实际上,我不了解如何使用序列化程序呈现HTML表单的请求流。请帮帮我或建议我首先学习什么,然后再继续学习。预先感谢。
答案 0 :(得分:0)
DRF响应(如果基于request.accepted_renderer.format
)。如果是html,则返回BrowsableAPI(有一些模板,基于引导程序)