How to Set up Static and Media Files in Django for Local Development



Python


In this article, we show how to set up static and media files in Django for local development.

By local development, we mean on your local server, on your own computer.

This is for local development, meaning you are not putting it out on a live server. It is simply for the phase while you are developing the site before live deployment.

So in order to set up our project so that we can have static files run in the project, we must do a number of things.

We must add code to our settings.py file and our urls.py file, as well as create a number of directories within our project.

All of this will be explained below.


settings.py File

So we are going to start with the settings.py file.

To our settings.py file, add the following code to the bottom of the file.



First, the STATIC_URL is the url that will appear in the full path name to the URL to the static files on our website. A comon value to have is, /static/

Next, we need a variable called, STATICFILES_DIRS. This variable, STATICFILES_DIRS, contains the static directory for our website. This is where we put all the directories of the static directory, such as css (for css files), javascript (for javascript files), images (for images on the site placed by the webmaster), videos (for videos on the site placed by the webmaster), etc. This is the directory where we put all of our static files, such as images, videos, CSS files, javscript files, etc.

In the root directory of your project (the same directory that has manage.py), create a directory called, static_my_project

Next, we have the variable, STATIC_ROOT. What this variable does is that all of the files from static_my_project gets carried over to static_cdn/static_root when we run the command, py manage.py collectstatic

The reason why we transfer files from one static directory to the other is because the static files of your website really should not be handled by Django. It should be handled by another web server, such as Apache, lighttpd (faster speed), nginx, or Amazon web services. It should not be handled by Django. It should be separate from Django. So, because it should be separate, we need to collect all the static files from our website and place them in a separate directory that contains all the static files. This is why we run the command, py manage.py collectstatic. We want to aggregate all the static files to a static file server, that handles all the static files. This is the whole point of the command, collectstatic

The STATIC_ROOT variable is really the directory or the static server that handles all of the static files on our site.

So within the static_cdn directory, create a directory called, static_root

Next, we create the variable, MEDIA_URL. The MEDIA_URL is set to, /media/

So, all media files on our site will have /media/ within its full path.

Media files are files that are uploaded by users of our website.

Remember, how we simulated a static server by creating the directory, static_cdn

This static server handles static files as well as media files.

So, when we create the variable, MEDIA_ROOT, we put this directory, media_root, within the static_cdn. So all media files on our site will be within the path, static_cdn/media_root. And within this media_root directory, we can create several directories, such as one for images, videos, etc.

So within the static_cdn directory, create a directory called, media_root

So this concludes the settings.py file.


urls.py File

Next, we go to the urls.py file within the project directory (the same directory that contains the settings.py file).

Within this file we want to add the following to the bottom of this file.



Remember, this is just for local development.

So DEBUG, in the settings.py file, should be set to TRUE, because we're testing.

So if settings.DEBUG is true, we want to add the following lines of code above.

This concludes the code to the urls.py file.


Purpose

The whole purpose of all of this is we want to put all of our static files within the static_my_project directory.

It's only when we're done putting in all of the static files that we run the command, py manage.py collectstatic

This commands transfers all of the files within our static_my_project directory to the static_cdn directory, which represents the live static server, when we actually go live.

So your main working directory for static files while coding the site is the static_my_project.

Running, py manage.py collectstatic, is like transferring those files to the live static server.

You just do that at the end.

So within the static_my_project directory, you create all your directories, such as css, javascript, images, videos.

And in the end, once you run the command, py manage.py collectstatic, all these directories with all their files gets transferred to the static_cdn directory for live deployment.

All static directories and files are transferred to the static_root directory in static_cdn. And all media directories and files are transferred to the media_root directory in static_cdn.

Everything is seamlessly transferred from the static_my_project directory to the static_cdn directory.

And this is how to set up static and media files for local development in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...