我正在尝试使用DRF api上传并获取对图像的响应。图像上传到指定的位置,但我想要的是获取图像的链接,以便我可以点击它,并在我得到响应时可以看到图像。 如果有人能帮助我纠正这个问题我会非常感激
models.py文件
class employees(models.Model):
name_of_employee = models.CharField(max_length=20, unique=True)
name_of_department = models.CharField(max_length=20, unique=True)
emp_id = models.IntegerField()
image = models.ImageField(upload_to='Images/', default='Images/None/No-img.jpg')
def __str__(self):
return '{} {}' .format(self.name_of_employee, self.name_of_department)
views.py文件
class employeeList(APIView):
def get(self, request):
employees1 = employees.objects.all()
serializer = employeesSerializer(employees1, many=True)
return Response(serializer.data)
def post(self, request):
serializer = employeesSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def delete(self, request, pk):
employees1 = self.get_object(pk)
employees1.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
class employeeDetail(APIView):
def get_object(self,pk):
try:
return employees.objects.get(pk=pk)
except employees.DoesNotExist:
raise Http404
def get(self, request,pk):
employees1 = self.get_object(pk)
employees1 = employeesSerializer(employees1)
return Response(employees1.data)
def put(self, request, pk):
employees1 = self.get_object(pk)
serializer = employeesSerializer(employees1, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_404_BAD_REQUEST)
def delete(self, request, pk):
employees1 = self.get_object(pk)
employees1.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
serializers.py文件
class employeesSerializer(serializers.ModelSerializer):
image = serializers.ImageField(max_length=None, use_url=True)
class Meta:
model = employees
fields = '__all__'
url.py文件
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^employees/', views.employeeList.as_view()),
url(r'^employees/(?P<pk>[0-9]+)/$', views.employeeDetail.as_view()),
]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
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/'
在运行服务器时我的回复是
HTTP 200 OK
Allow: GET, POST, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
[
{
"id": 1,
"image": "/media/Images/share-1.jpg",
"name_of_employee": "Abhinav",
"name_of_department": "electrical",
"emp_id": 1
},
现在我想获取图像字段上的链接,以便我可以点击它并查看存储的图像
提前致谢
答案 0 :(得分:0)
我认为你想要返回绝对而不是相对路径。我使用以下序列化程序的自定义图像实现来返回绝对路径:
class PhotoSerializer(serializers.ModelSerializer):
image_upload = serializers.ImageField(write_only=True)
image = serializers.SerializerMethodField('get_image_url')
class Meta:
model = Photo
fields = ('id', 'image', 'image_upload')
def get_image_url(self, obj):
'''
Return an absolute url for the image
'''
return self.context['request'].build_absolute_uri(str(obj.image))
这可能不是您想要的,因为它不会使用相同的字段进行上传/下载。