3

I'm learning Django Rest Framework & facing some issue.

I have a small Task App API.

I'm able to save an image on the server but not able to get Image from API image hyperlink.

My Model.py

class Task(models.Model):
    task_name = models.CharField(max_length=30)
    task_desc = models.CharField(max_length=30)
    image = models.ImageField(upload_to = 'pic_folder/', default = 'pic_folder/None/no-img.jpg')

Views.py

class TaskViewSet(viewsets.ModelViewSet):
    queryset = Task.objects.all()
    serializer_class = TaskSerializer

serializer.py

class TaskSerializer(serializers.HyperlinkedModelSerializer):
    image = serializers.ImageField(
            max_length = None, use_url=True
        )   
    class Meta:
        model = Task
        fields = ('id','task_name','image')

settings.py

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

APP Structure:-

TaskAPI  (Base Dir)
    TaskAPP
     media

I can Make a Request For POST successfully & It show correct image link but when i click on image link it gives error.

enter image description here

Error:- enter image description here Please suggest how to get Image from link.

3
  • error? post the traceback. Commented Mar 9, 2016 at 11:56
  • I think the GET is working fine since you are getting image's URL. Maybe the problem is in the form? Can you show the HMTL code?? Commented Mar 9, 2016 at 11:58
  • I agree. but is this link show me an image in the browser ? like any other server-side image link behave in a browser. Commented Mar 9, 2016 at 12:02

1 Answer 1

4

You did not included the media url in your urls.py

from django.conf.urls.static import static
from django.conf import settings

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

see the docs on serving media and static files.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.