0
我無法使用Django Rest-framework通過POSTMAN上傳圖像,但是我可以使用Django Admin來做同樣的事情。當我嘗試調試代碼時,發現serializer.is_valid()
總是錯誤的。我無法使用Django Rest-framework上傳圖像
這些都是我的部分代碼,
views.py
class imageuploadView(APIView):
def post(self, request,format=None):
serializer=ImageSerializer(data=request.data)
print serializer
if serializer.is_valid():
serializer.save()
return Response(data={"detail":"Image Uploaded !"})#data
else:
return Response(data={"detail":"Not Uploaded"})
models.py
class ImageUploadModel(models.Model):
usr_id=models.IntegerField()
upImg=models.ImageField(upload_to='media/')
caption=models.CharField(max_length=50,default=None)
serializer.py
class ImageSerializer(serializers.ModelSerializer):
upImg = serializers.ImageField(max_length=None,use_url=True)
class Meta:
model=ImageUploadModel
fields=('usr_id','upImg','caption')
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
'/static/',
)
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
這是我Source Code
嘗試過,但剩下的問題有:( –
我更新的問題與我的源代碼,這可能對你有用 –
@JerinPeterGeorge您是否曾嘗試通過DRF提供的可瀏覽API上傳圖像,嘗試一下並說出它如何與圖像協同工作並添加'serializer.errors'消息而不是源代碼您的代碼看起來正確 –