Storing Static Files in Django

We have the templates folder to put all of our HTML files, but where do we put the other static files like CSS and Javascript so that we can use it on our pages? To do this in django, we have to create a static/ folder and then tell django where the folder is so that it knows where to look.

Making a Static Folder

Make the static/ folder at the same level as the manage.py. Then, within the static/ folder, make a css/ folder and a js/ folder. We will be putting our css and javascript in these folders. It should look like this:

Linking the Static Folder in Settings

Similarly to when we linked the templates folder, we have to link the static folder to our application by telling settings.py where it is. To do this, add the following code to the bottom of your settings.py:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

This will let django know to look in the static folder whenever we tell it to grab a static file. Now that we're setup, let's dive into actually making css and javascript files!

results matching ""

    No results matching ""