Python Django Rest框架模型视图响应不正确
我对drf有些问题,有人能指出我做错了什么吗 这是我的视图功能,我还实现了easy_缩略图,以便在提出此类请求时可以裁剪图像Python Django Rest框架模型视图响应不正确,python,django,django-rest-framework,Python,Django,Django Rest Framework,我对drf有些问题,有人能指出我做错了什么吗 这是我的视图功能,我还实现了easy_缩略图,以便在提出此类请求时可以裁剪图像 from __future__ import unicode_literals from django.shortcuts import render from rest_framework import viewsets from rest_framework.response import Response from .models import Image from
from __future__ import unicode_literals
from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.response import Response
from .models import Image
from .serializers import ImageSerializer
from easy_thumbnails.files import get_thumbnailer
class ImageViewSet(viewsets.ModelViewSet):
queryset = Image.objects.all()
serializer_class = ImageSerializer
def retrieve(self, request, pk=None):
height = request.GET.get('height', None)
width = request.GET.get('width', None)
print("height = {}".format(height))
print("width = {}".format(width))
print("id = {}".format(pk))
img = Image.objects.get(pk = pk)
if height and width:
options = {'size': (height, width), 'crop': True}
thumb_url = get_thumbnailer(img.image).get_thumbnail(options).url
else:
thumb_url = get_thumbnailer(img.image).url
return Response(thumb_url)
如果转到http://127.0.0.1:8000/api/images/
它会返回一个图像列表
和http://127.0.0.1:8000/api/images/1/?height=320&width=420
返回如下响应
HTTP 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
"/media/10438039923_2ef6f68348_c.jpg.320x420_q85_crop.jpg"
我想要一个这样的带有字段名的响应
{
"title": "Item 1",
"description": "Description 1",
"image": "http://127.0.0.1:8000/media/10438039923_2ef6f68348_c.jpg"
}
如何解决此问题?我是drf和django的新手
这是我的序列化程序类
from rest_framework import serializers
from .models import Image
class ImageSerializer(serializers.ModelSerializer):
class Meta:
model = Image
fields = [
'title',
'description',
'image',
]
你可以这样做
return Response({"title": img.title, "description": img.description,
"image": img.image, "thumbnail": thumb_url})
你可以这样做
return Response({"title": img.title, "description": img.description,
"image": img.image, "thumbnail": thumb_url})
使用以下命令:
def retrieve(self, request, pk=None):
height = request.query_params.get('height', None)
width = request.query_params.get('width', None)
img = self.get_object()
if height and width:
options = {'size': (height, width), 'crop': True}
thumb_url = get_thumbnailer(img.image).get_thumbnail(options).url
else:
thumb_url = get_thumbnailer(img.image).url
serializer = self.get_serializer(img)
# insert thumb_url into data if you need it
response_dict = {}
response_dict.update(serializer.data)
response_dict['image'] = thumb_url
return Response(response_dict)
使用以下命令:
def retrieve(self, request, pk=None):
height = request.query_params.get('height', None)
width = request.query_params.get('width', None)
img = self.get_object()
if height and width:
options = {'size': (height, width), 'crop': True}
thumb_url = get_thumbnailer(img.image).get_thumbnail(options).url
else:
thumb_url = get_thumbnailer(img.image).url
serializer = self.get_serializer(img)
# insert thumb_url into data if you need it
response_dict = {}
response_dict.update(serializer.data)
response_dict['image'] = thumb_url
return Response(response_dict)
这不起作用,我的serializer_类已经定义,我需要找到一种方法来使用它。UnicodeDecodeError at/api/images/1/'ascii'编解码器无法解码第11位的字节0x89:序号不在范围(128)内,但您也需要缩略图url,不是吗?这不起作用,我的序列化器类已经定义,我需要找出一种使用方法。UnicodeDecodeError at/api/images/1/'ascii'编解码器无法解码第11位的字节0x89:序号不在范围(128)内,但您也需要缩略图url,不是吗?感谢您的解决方案,它可以工作,但有一个问题,当我发送参数来裁剪图像时,我仍然会得到图像url,而不是缩略图url:我得到:{“title”:“项目1”,“描述”:“描述1”,“图像”:“”}以前,如果我将高度和宽度传递给get请求,我会得到一个裁剪过的图像url。例如:127.0.0.1:8000/api/images/1/?height=320&width=420我会得到这个HTTP 200 OK Allow:get、PUT、PATCH、DELETE、HEAD、OPTIONS内容类型:application/json Vary:Accept”/媒体/10438039923_2ef6f68348_c.jpg.320x420_q85_crop.jpg“@ZeeS使用以下内容:。我没有使用easy_缩略图,请查看它的文档,看看它是否可以为您提供完整的url。非常感谢您的帮助,非常感谢,我确实学到了很多东西。感谢您的解决方案,它可以工作,但是有一个问题,当我发送params来裁剪图像时,我仍然会得到图像url,而不是thumb_url:我得到这个:{“title:“Item 1”,“description:”description:“description 1”,“image:”}以前,如果我将高度和宽度传递给get请求,我会得到一个裁剪过的图像url。例如:127.0.0.1:8000/api/images/1/?height=320&width=420我会得到这个HTTP 200 OK允许:get、PUT、PATCH、DELETE、HEAD、OPTIONS内容类型:application/json Vary:Accept”/media/10438039923_2ef6f68348_c.jpg.320x420_q85_crop.jpg“@ZeeS使用这个:。我没有使用简单的缩略图,请查看它的文档,看看它是否能为您提供完整的url。非常感谢您的帮助,非常感谢,我确实学到了很多东西。