Django XML 文件上传 API 未被调用且 [错误]:不允许使用方法

发布于 2025-01-12 05:37:30 字数 1934 浏览 0 评论 0原文

我正在开发 Django 项目,该项目将通过 REST API 上传 XML 文件,但我面临问题,不确定它是什么!

我正在分享我为 [API] 的不同模块编写的代码

网址.py

path('xml-file/', xmlfileview.as_view(), name='xml-file')

序列化器.py

class xmlfileSerializer(serializers.ModelSerializer):
file = serializers.FileField()
class Meta:
    model = xmlColumns
    fields = ['inputfile']

视图.py

class xmlfileview(generics.CreateAPIView):
serializer_class = xmlfileSerializer
# parser_classes = (FileUploadParser,)

def post(self, request, *args, **kwargs):
    token = request.META.get('HTTP_AUTHORIZATION')
    try:  #
        payload = jwt.decode(token,
                             settings.SECRET_KEY)  # payload is a dictionary to decode the token to get the user id
        user = User.objects.get(id=payload['user_id'])  # user is a function to get the user model

        if user.is_verified:
            print(user)
            # serializer = self.get_serializer(data=request.data)
            # serializer.is_valid(raise_exception=True)
            # serializer.save()
            # file = serializer.validated_data['file']
            # print(file)
            file_obj = request.data['file']
            df = pd.read_xml(file_obj)
            for col in df.columns:
                print(col)
            # print(df)
            return Response({'msg': 'file sent'}, status=status.HTTP_400_BAD_REQUEST)

    except jwt.ExpiredSignatureError as identifier:
        return Response({'error': 'Activation Expired'}, status=status.HTTP_400_BAD_REQUEST)
    except jwt.exceptions.DecodeError as identifier:
        return Response({'error': 'Invalid token'}, status=status.HTTP_400_BAD_REQUEST)

模型.py

class xmlColumns(models.Model):
    ipfile = models.FileField()

I am working on Django project that will upload XML file via rest API and i am facing issue and not sure what it is !

i am sharing my Codes that i have written for Different Modules for [API]

URL.py

path('xml-file/', xmlfileview.as_view(), name='xml-file')

Serializers.py

class xmlfileSerializer(serializers.ModelSerializer):
file = serializers.FileField()
class Meta:
    model = xmlColumns
    fields = ['inputfile']

Views.py

class xmlfileview(generics.CreateAPIView):
serializer_class = xmlfileSerializer
# parser_classes = (FileUploadParser,)

def post(self, request, *args, **kwargs):
    token = request.META.get('HTTP_AUTHORIZATION')
    try:  #
        payload = jwt.decode(token,
                             settings.SECRET_KEY)  # payload is a dictionary to decode the token to get the user id
        user = User.objects.get(id=payload['user_id'])  # user is a function to get the user model

        if user.is_verified:
            print(user)
            # serializer = self.get_serializer(data=request.data)
            # serializer.is_valid(raise_exception=True)
            # serializer.save()
            # file = serializer.validated_data['file']
            # print(file)
            file_obj = request.data['file']
            df = pd.read_xml(file_obj)
            for col in df.columns:
                print(col)
            # print(df)
            return Response({'msg': 'file sent'}, status=status.HTTP_400_BAD_REQUEST)

    except jwt.ExpiredSignatureError as identifier:
        return Response({'error': 'Activation Expired'}, status=status.HTTP_400_BAD_REQUEST)
    except jwt.exceptions.DecodeError as identifier:
        return Response({'error': 'Invalid token'}, status=status.HTTP_400_BAD_REQUEST)

Models.py

class xmlColumns(models.Model):
    ipfile = models.FileField()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文