Django - Add Static File

When building web applications, you probably want to add some static files like images or css files.

Start by creating a folder named static in your project, the same place where you created the templates folder:

Add a CSS file in the static folder, the name is your choice, we will call it index.css

Open the CSS file and insert the following:


body {
  background-color: lightblue;
  font-family: verdana;
}


Modify the Template

Now you have a CSS file, with some CSS styling. The next step will be to include this file in a HTML template:

Open the HTML file and add the following:


{% load static %}


And;


link rel="stylesheet" href="{% static 'myfirst.css' %}">



{% load static %}
!DOCTYPE html>
html>
link rel="stylesheet" href="{% static 'myfirst.css' %}">
body>

Hello World

/body>
/html>


Restart the server for the changes to take effect:


python3 manage.py runserver 


Latest Blog