3

我面臨的問題在返回的圖像網址,這是不正確的。Django休息框架API - 圖像URL不正確返回

我返回圖像的URL是"http://127.0.0.1:8000/showimage/6/E%3A/workspace/tutorial_2/media/Capture1.PNG" 但我需要

"http://127.0.0.1:8000/media/Capture1.PNG" 

enter image description here

,當我在image_url然後圖像在新的瀏覽器選項卡 開放,但目前它的顯示錯誤,請點擊: enter image description here 圖.py

from showimage.models import ShowImage 
from showimage.serializers import ShowImageSerializer 
from rest_framework import generics 

# Create your views here. 

    class ShowImageList(generics.ListCreateAPIView): 
     queryset = ShowImage.objects.all() 
     serializer_class = ShowImageSerializer 

    class ShowImageDetail(generics.RetrieveUpdateDestroyAPIView): 
     queryset = ShowImage.objects.all() 
     serializer_class = ShowImageSerializer 

model.py

from __future__ import unicode_literals 

from django.db import models 
from django.conf import settings 


# Create your models here. 

class ShowImage(models.Model): 
    image_name = models.CharField(max_length=255) 
    image_url = models.ImageField(upload_to=settings.MEDIA) 

serializer.py

from rest_framework import serializers 
from showimage.models import ShowImage 

class ShowImageSerializer (serializers.ModelSerializer): 
    class Meta: 
     model = ShowImage 
     fields = ('id', 'image_name', 'image_url') 

settings.py

MEDIA=os.path.join(BASE_DIR, "media") 

urls.py

from django.conf.urls import url, include 
from django.contrib import admin 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^showimage/', include('showimage.urls')), 
] 

我是新的python和django-rest-framework。 也請告訴我,如何在先進的擴展模型或序列化類

感謝您的援助之手..

+0

你說的 「擴展模式」 的意思。你想實現什麼? –

+0

所有你的圖片網址看起來都很奇怪,錯誤......你的錯誤圖片(你應該包括文本btw)中的那個圖片試圖鏈接到一個E盤,這對於那個請求中涉及的每個人都是無用的。然後你的其他人不應該關心它託管的端口或域名。 – Sayse

+0

謝謝,我的問題解決了。 :-) –

回答

4

你可能想試試這個在您的設置:

MEDIA_URL = '/media/' 
MEDIA_ROOT=os.path.join(BASE_DIR, "media") 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^showimage/', include('showimage.urls')), 
] 

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 

而在你的機型:

class ShowImage(models.Model): 
    image_name = models.CharField(max_length=255) 
    image_url = models.ImageField(upload_to="") # or upload_to="images", which would result in your images being at "http://127.0.0.1:8000/media/images/Capture1.PNG" 
1

你的代碼似乎是正確的,只有一件事你已經通過settings.MEDIA上傳圖片。您無需在上傳中傳遞settings.MEDIA。

試試這個

image_url = models.ImageField(upload_to='Dir_name') 

DIR_NAME將創建的時候,你會運行的腳本。

0

最後,我解決的 @Remi 感謝幫助@Remi 但有些改變我這樣做,我詳細的解決方案和解決這個問題,這條路塊。

settings.py

STATIC_URL = '/static/' 
MEDIA_URL = '/media/' 
MEDIA_ROOT=os.path.join(BASE_DIR, "media") 

urls.py

from django.conf.urls import url, include 
from django.contrib import admin 
from django.conf import settings 
from django.conf.urls.static import static 

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^showimage/', include('showimage.urls')), 
] 

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)