2017-05-16 40 views
0

當我請求圖像http://127.0.0.1:8000/api/images/1/或params中傳遞用於裁剪http://127.0.0.1:8000/api/images/1/?height=320&width=420Django的REST框架 - 附加主機名到響應

響應我得到的是:

{ 
     "image": "/media/10438039923_2ef6f68348_c.jpg", 
     "description": "Description 1", 
     "title": "Item 1" 
    } 

而作爲http://127.0.0.1:8000/api/images/

的迴應是:

 { 
      "title": "Item 1", 
      "description": "Description 1", 
      "image": "http://127.0.0.1:8000/media/10438039923_2ef6f68348_c.jpg" 
     }, 
     { 
      "title": "Item 2", 
      "description": "Description 2", 
      "image": "http://127.0.0.1:8000/media/ALLEY-stock1502.jpg" 
     }, 

爲什麼不容易的縮略圖返回主機名,我如何追加基地網址的答覆?

這裏是我的views.py

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.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) 
     response_dict = {} 
     response_dict.update(serializer.data) 
     response_dict['image'] = thumb_url 
     return Response(response_dict) 
+0

請添加串行代碼。 – iamkhush

回答

0

或在您的設置更改MEDIA_URL

MEDIA_URL = '/media/' 

MEDIA_URL = 'http://127.0.0.1:8000/media/'