Serving Files in Django

Serving Files in Django

Advert

A web application comprises of different file-types like media files for the application, files uploaded by the user and more, so is the case with a typical Django application, these files can be classified into two Static Files and Media Files as what Django calls it.

  1. Static Files : It comprises of files used by the application which includes the CSS, Javascript, images, fonts etc.
  2. Media Files : These includes files being uploaded from usage of application like profile image of user and other files.

In django these have to be managed differently depending on the environment ( Production or Development), I'll walk through how static and media files are server in both environments.

This article is focused on how files are served so I'll not be concentrating on configuration or setup of files or server .

These are the variables django uses in settings.py for handling files, it's good to understand what each of the means.

  • STATICFILES_DIRS : Contains the list of directories where static files can be stored. Django allows static files to be stored in multiple location withing the apps so refer Django Documentation for example and its configurations.
  • STATIC_ROOT : The absolute path to the directory where static files are to be collected ( Not required in development environment ). Eg : "/var/www/html/static/"
  • STATIC_URL : Reference URL for browser to located static files. Eg : "/static/" or "http://static.example.com/"
  • MEDIA_ROOT : The absolute path to the directory where media files are to be uploaded or stored. Eg : "/var/www/html/media/"
  • MEDIA_URL : Reference URL for browser to located media files. Eg : "/media/" or "http://media.example.com/"

Development Environment

Testing or development version, here the server is deployed using python manage.py runserver.

Static Files

From Django 1.9 and above there is no additional task involved in serving static files in development environment other than steps involved in configuring the static files ( steps are mentioned in djangoproject.com) , manage.py runserver performs the rest.

Media Files

We do have a small tweak to server medial files. The urls.py in main directory has to me informed the location &apm; about the media files. Add this to the end of urls.py

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The production version uses a different settings so we can make urls.py detect the environment and add this setting only in case of development.

if settings.DEBUG is True:
  urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Production Environment

The D-task, a little confusing but you can do this if you reached up to here :).

Django required a few steps to be performed before files can be setup in production server, because django doesn't server files like PHP or ASP, it's done using a web server (Nginx or Apacha) or via a CDN (content delivery network).

I'm not gonna be focusing on how to set up a production server for django here ( I'll make a new post for it ).

Static Files

Django allows static files inside the respective apps to make development easy, so first task is to collect them to a single location.

Set STATIC_ROOT as absolute path to directory inside your web server's root.

Example:
STATIC_ROOT =' /var/www/html/static/'

Once the static is path is set python manage.py collectstatic will collect all the static files to this location, which can be served by the web-server.

STATIC_URL is to be set the web location of the static files directory.

Example:
STATIC_URL =  'http://static.example.com/'
or 
STATIC_URL =  'http://example.com/static/'

If you make further changes to the Application and static files make sure you execute collectstatic again so the files are updated.

Media Files

Similar to static files, media files is also served by web server. But unlike static files task is simple because media files are user generated so you don't have to collect or mange it during deployment. They are stored directly to the location where they are served unless you do processing or other complex tasks to the upload :D.

Set MEDIA_ROOT as absolute path to directory inside your web server's root.

Example:
MEDIA_ROOT =' /var/www/html/media/'

Ensure the directory has right for public to read and write to allow uploads.

MEDIA_URL is to be set the web location of the static files directory.

Example:
MEDIA_URL =  'http://media.example.com/'
or 
MEDIA_URL =  'http://example.com/media/'

Django is ready to handle files in production environment.

I made this tutorial explaining the tasks to my friend, he's an experience developer so I think there can be areas which needs explanation or help let me know in comments I'll try to help.

Comments

Wow ! you have someting to tell us. That's great! Please keep in mind that comments are moderated, we employ rel="nofollow" for links, avoid using a spammy word or a domain in name field, it might end up as a Spam. Thanks for reading.

Last 5 Articles

All Articles >

  1. How to Remove an App from Django Project

     
  2. Parse Time HH:mm:ss without date in javascript

     
  3. Software Testing

     
  4. Best WordPress Themes For Photographers And Photography

     
  5. Problems That Affect Conversion Rates

     

News Letter

Subscribe to our email newsletter for useful tips and valuable resources, sent out every new article release.