Hari Krishnan
Hari Krishnan

Reputation: 6312

How to handle ImageFiled in Django rest framework?

I am new to django rest framework and trying to handle image data. After adding an ImageFiled i can upload image to my model. But on accessing that image via browser, the browser returns a page not found error.

My model

from __future__ import unicode_literals
from django.conf import settings
from django.db import models
from rest_framework import serializers
from urlparse import urljoin


class Item(models.Model):
    name = models.CharField(max_length=64, verbose_name='name')
    address = models.CharField(max_length=64, verbose_name='address')
    image = models.ImageField(max_length=None, null=True)

def __str__(self):
    return self.name

Model serializer

class ItemSerializer(serializers.ModelSerializer):

class Meta:
    model = Hotel
    fields = ('name','address','id','image')

The field image returns a url and on hitting that url the browser returns a 404 error. So what is the proper way to implemet an api with image data?

Upvotes: 2

Views: 68

Answers (1)

zaidfazil
zaidfazil

Reputation: 9245

You may need to add in your urls.py,

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

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 3

Related Questions