10

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?

jul
  • 36,404
  • 64
  • 191
  • 318
  • I think its a good idea to serve those files via Apache or a similiar webserver. If you need further control like user permissions, mod_xsendfile could be an option. – Jingo Jul 05 '12 at 10:41

1 Answers1

4

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

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Do you have any example about how to configure it to serve from apache? – jul Jul 05 '12 at 10:55
  • use the same configuration that you're using for your static files. or are you asking how to configure apache to serve files in general? – scytale Jul 05 '12 at 11:33
  • I'm using webfaction hosting and I followed the instructions for serving static media given in http://docs.webfaction.com/software/django/getting-started.html. This is for use with `django.contrib.staticfiles`, but I need another directory in which I can upload files and serve them, without using `django.contrib.staticfiles`. I'll ask in webfaction specific forum. Thanks. – jul Jul 05 '12 at 12:31