Python Django – How to add css to django application

So, some time ago I have started to create apps in Django with Python. It is really fun and amazing, whatΒ one can achieve with this package πŸ™‚

Python

E.g., I have generated a small app, counting words and analyzing their repeatability, but I will write about it later. Now, you can take a look at GitHub, if you want to.

Anyway, as soon as I started with writing HTML in Django, the first thing I have encountered is that it is not that easy to add a separate style.css file to it. I was adding it with the standard way, but it simply was not working. Thus, I have decided to make a small research in the net, and it seemed to be quite a big issue for some people as well. I have tried some of the recipes, but it did not work out as I wanted it to. Thus, today, while I was looking for some information for staticfiles in Django, I found a way to do it and I managed it πŸ™‚

So, for all the people out there, who try to search for “how to add css to django”, here is the correct sequence:

  1. At the bottomΒ of the file settings.py, write the following:
    STATIC_URL = '/static/'
    STATIC_ROOT = os.path.join(BASE_DIR,  'static')
    

    The first line should be there, so just add the second line.

  2. In the folder of the django app, create a folder named “static”. You can find the exact location of my static folder from my app for counting words here:
    β”œβ”€β”€ db.sqlite3
    β”œβ”€β”€ manage.py
    β”œβ”€β”€ wordapp
    β”‚Β Β  β”œβ”€β”€ __init__.py
    β”‚Β Β  β”œβ”€β”€ __pycache__
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ __init__.cpython-34.pyc
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ settings.cpython-34.pyc
    β”‚Β Β  β”‚Β Β  β”œβ”€β”€ urls.cpython-34.pyc
    β”‚Β Β  β”‚Β Β  └── wsgi.cpython-34.pyc
    β”‚Β Β  β”œβ”€β”€ settings.py
    β”‚Β Β  β”œβ”€β”€ urls.py
    β”‚Β Β  └── wsgi.py
    └── wordcountapp
        β”œβ”€β”€ admin.py
        β”œβ”€β”€ __init__.py
        β”œβ”€β”€ migrations
        β”‚Β Β  β”œβ”€β”€ __init__.py
        β”‚Β Β  └── __pycache__
        β”‚Β Β      └── __init__.cpython-34.pyc
        β”œβ”€β”€ models.py
        β”œβ”€β”€ __pycache__
        β”‚Β Β  β”œβ”€β”€ admin.cpython-34.pyc
        β”‚Β Β  β”œβ”€β”€ __init__.cpython-34.pyc
        β”‚Β Β  β”œβ”€β”€ models.cpython-34.pyc
        β”‚Β Β  β”œβ”€β”€ urls.cpython-34.pyc
        β”‚Β Β  └── views.cpython-34.pyc
        β”œβ”€β”€ static
        β”‚Β Β  └── css
        β”‚Β Β      └── style.css
        β”œβ”€β”€ templates
        β”‚Β Β  └── index.html
        β”œβ”€β”€ tests.py
        β”œβ”€β”€ urls.py
        └── views.py
    
  3. So far so good. Now go to the html file in the templates folder (in my app it is index.html), open it and on the top of it write “{% load staticfiles %}”. Write this even before openning the tag, write it on the first line.
  4. Then within the <head></head> tag writeΒ  <link rel=”stylesheet” href=”{% static ‘css/style.css’ %}”> My head tag currently looks like this:
      <head>
        <link rel="stylesheet"  href="{%  static  'css/style.css'  %}">
        <title>Linguist</title>
      </head>
    

That is all! Enjoy it!

πŸ™‚