Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve user-uploaded files in Django?

Tags:

django

I'm writing a Django app in which the users can upload images, and I'm not sure how to serve them. I can see two ways:

1- upload them as static files: upload them in the static folder in my project's directory, and then run python manage.py collectstatic, but I don't know how to run this command automatically every time a file is uploaded, and this seems to be a lot of processing because every time the server will delete and reload everything in my static app.

2- use django.views.static.serve in my urls.py:

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT,
    }),

but the doc does not recommend it for production.

What's the recommended way to serve user-uploaded files?

like image 742
jul Avatar asked Jul 05 '12 10:07

jul


1 Answers

Your uploaded files go in MEDIA_ROOT and can be served using MEDIA_ROOT. You need to set these parameters in settings.py.

Your development you can serve them though django dev server, but you can configure it to serve from apache or other server.

Refer Django managing stored files

like image 137
Rohan Avatar answered Oct 21 '22 03:10

Rohan