Skip to content

Linking Static Files

sachin soman edited this page Jun 12, 2020 · 2 revisions

Staic File Linking


Static file are handled a bit different in Django suppose that we need to link a css file to webpage normally we would do the following

    <link rel="stylesheet" type="text/css" href="/main.css">

However, in Django this doesn't work we need to like previously create a different type of file system. Inside our blog project, we need to create a file structure called static in that again have to create another directory with the name of our project and in our case blog, so our directory will have the following structure static/blog/ inside that, we add our css file.

Screenshot 2020-06-12 at 16 14 01

after that we need to add this line to each page we make a line this is usually the first line in the page where we want to load the file

{% load static %}

this line of code enables us to load a static file. Now we can do the following.

    <link rel="stylesheet" type="text/css" href="{% static 'blog/main.css' %}">

The word static will create a hard link to the static folder and then link our css and now the page should have the css