I have created a django rest model which includes a FileField.
media = models.FileField(upload_to='media/%Y/%m/%d/', null=True, blank=True)
I also implemented serializer and ListCreateApiView. There I can able to upload a file. On POST request rest server uploads the file in folder and returns me the url. However, on get request, server return json content with the url. If I use the url for get request, server respond Page Not Found. How to download the uploaded file using django rest? Do I have to create separate view for the same? If so, how to do that?
Edit:
The resulting url is
http://localhost:8000/message/media/2015/12/06/aa_35EXQ7H.svg
You have to define
MEDIA_ROOT
,MEDIA_URL
and registerMEDIA_URL
inurlpatterns
to allow Django server to serve these files.Follow these steps:
settings.py
file:Append this in your main
urls.py
file to serve media files :Also, you don't have to add
media
again inupload_to
attribute because it's prepended byMEDIA_URL
, then the url will be/media/media/
.Here is a correct example:
and the url of the media will be: