Django XML 文件上传 API 未被调用且 [错误]:不允许使用方法
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论