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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With