How to serve user uploaded files in Django?

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

1- upload them as static files: upload them to the static folder in my project directory and then run python manage.py collectstatic , but I don’t know how to run this command automatically every time the file loads, and that seems to be a lot processing, because every time the server uninstalls and restarts everything in my static application.

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 document does not recommend it for production.

What is the recommended way to serve user uploaded files?

+6
source share
1 answer

The downloaded files are included MEDIA_ROOT and can be used with MEDIA_ROOT . You need to set these parameters in settings.py.

Your development can serve them, although the server is django dev, but you can configure it to serve from apache or another server.

Contact Django to Manage Stored Files

+2
source

Source: https://habr.com/ru/post/919741/


All Articles