Handling Media Files in Django
Last updated on July 27, 2020
In Django, files which are uploaded by the user are called Media or Media Files. Here are some examples:
- A user uploaded image, pdfs, doc files etc while publishing a post.
- Images of products in an e-commerce site.
- User's profile image. etc...
Just as with static files, to serve media files we have do add some configurations in our settings.py
file.
Media Files Configurations #
Media files depend upon two configurations:
MEDIA_ROOT
MEDIA_URL
None of these are set by default.
MEDIA_ROOT Setting #
It contains the absolute path to the file system where Media files will be uploaded. It accepts a string, not a list or tuple.
Create a new directory named media
inside Django project root directory (TGDB/django_project
), the same place where manage.py
is located.
Open settings.py
file and add the following code to the end of the file, just below STATICFILES_DIRS
which we had set earlier.
TGDB/django_project/django_project/settings.py
1 2 3 4 5 6 | #...
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
|
MEDIA_URL Setting #
This setting works similar to STATIC_URL
and is used to access media files. Just below the MEDIA_ROOT
setting add the following code to the end of settings.py
file.
TGDB/django_project/django_project/settings.py
1 2 3 | #...
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
|
Download this image and save it as python.png
in the media
directory. To access the file visit http://127.0.0.1:8000/media/python.png
. You will get an HTTP 404 error like this:
But why?
The problem is that Django development server doesn't serve media files by default. To make Django development server serve static we have to add a URL pattern in sitewide urls.py
file.
First open urls.py
in the Django project configuration directory, i.e TGDB/django_project/django_project
. If you are following along this tutorial closely, your urls.py
file should look like this:
TGDB/django_project/django_project/urls.py
1 2 3 4 5 6 7 8 | from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'', include('blog.urls')),
url(r'^admin/', admin.site.urls),
url(r'^cadmin/', include('cadmin.urls')),
]
|
Modify the urls.py
file as follows:
TGDB/django_project/django_project/urls.py
1 2 3 4 5 6 7 8 9 10 | from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'', include('blog.urls')),
url(r'^admin/', admin.site.urls),
url(r'^cadmin/', include('cadmin.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
|
Now visit http://127.0.0.1:8000/media/python.png
again, this time you should be able to see the image.
Just as with static files, the method employed here to serve media files is not recommended in the production environment. In fact, we will use Nginx server to serve both media and static files. We will discuss how to do this in Django Deployment lesson.
Note: To checkout this version of the repository type git checkout 25a
.
Load Comments