0

I'm trying to return a uploaded file to the Client.

models.py

file = models.FileField(_('file'), db_index=True, null=True, blank=True, upload_to='files/')

views

class ContentInfoViewSet(viewsets.ModelViewSet):
    queryset = ContentInfo.objects.all()
    serializer_class = ContentInfoSerializer
    http_method_names = ['get']

    @detail_route(methods=['get']) //this is just for testing
    def files(self, request, pk=None):
        return Response(pk, status=status.HTTP_200_OK)

Here I was just trying with a "files" route.

When I try to get "content-info". It works nicely:

[
  {
    "url": "http://127.0.0.1:8000/users/content-info/1/",
    "id": 1,
    "date": "2017-01-27T16:21:41.976289Z",
    "title": "Hey Hey",
    "image_url": "",
    "content_url": "",
    "file": null
  },
  {
    "url": "http://127.0.0.1:8000/users/content-info/3/",
    "id": 3,
    "date": "2017-03-21T12:09:32.984119Z",
    "title": "NWE",
    "image_url": "",
    "content_url": "",
    "file": "http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf"
  }
]

But that URL doesn't work. Even if I make a get with Authorization. I don't know what I'm doing wrong. It doesn't find the page. And it's logic, because it's not in the urls.py (I mean http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf)

This solution would be great: pdf

when you open the link, it shows the pdf. I thought that it would happen when I follow this link "http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf"

Chuck Aguilar
  • 1,998
  • 1
  • 27
  • 50
  • You are using Django Rest Framework right? Maybe [this post](http://stackoverflow.com/questions/2294507/how-to-return-static-files-passing-through-a-view-in-django) will be helpful – code22 Mar 21 '17 at 12:31
  • Yeah, sorry, I forgot to target it. – Chuck Aguilar Mar 21 '17 at 12:41
  • Is it there a better way to do it? (Even if I don't use the fileField) I just wanna show the file in Browser. The point is that I nee to show either HTML, pdf or MP3... and with the content-type I gotta specify the type – Chuck Aguilar Mar 21 '17 at 12:56
  • FileField is just the way file will be stored and not about how it's going to be displayed. You can also use [static.serve](https://docs.djangoproject.com/en/1.10/ref/views/#django.views.static.serve) to serve all files from a directory but then I think you'll loose permission control. – code22 Mar 21 '17 at 13:10
  • Ok, I'll see it :). Thanks I didn't know it exists – Chuck Aguilar Mar 21 '17 at 13:11
  • Hmm I don't know how to use it. It's not working for me, I get a 404. :( I changed media for static and put "files" in static. So it should be ".../static/files/BIAM-51.pdf", but I get a "not found" error – Chuck Aguilar Mar 21 '17 at 13:33

1 Answers1

0

AS code22 said, I was able to do it with media_root and now it works really nicely.

I created the folder "media" in my project.

proj
|---proj
|---media
|---static
|---app

then, I added this to settings.py

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

this in the urls.py (of the whole project)

from proj import settings
from django.views.static import serve

urlpattern = [
    ...
    url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
]

in models.py

file = models.FileField(_('file'), db_index=True, null=True, blank=True, upload_to='files/')

then python3 manage.py makemigrations and python3 manage.py migrate

And the file (after upload) is here: http://api.mydomain.com/media/files/myfile.pdf" and that's it :)

Chuck Aguilar
  • 1,998
  • 1
  • 27
  • 50
  • can anyone now just access any file in `media/`? If so, please say so in your answer because this is probably not what people want to be doing... – TMOTTM Aug 09 '21 at 23:04
  • It depends on your global permissions. In my case, you'll need to be authorized to access any endpoint of the domain. – Chuck Aguilar Aug 12 '21 at 10:04
  • https://docs.djangoproject.com/en/dev/ref/views/#serving-files-in-development Link specifically says it is for development. – Sardar Faisal Jun 02 '22 at 07:57