Add Static Files
Learn all about Add Static Files in this comprehensive tutorial.
- •When building web applications, you probably want to add some static files like images or css files.
- •Now you have a CSS file, with some CSS styling.
- •For the rest of this tutorial, we will run with DEBUG = False, even in development, because that is the best way to learn how to work with Django.
- •That is right, the example still does not work.
Create Static Folder
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:
The name of the folder has to be static.
Add a CSS file in the static folder, the name is your choice, we will call it myfirst.css in this example:
Open the CSS file and insert the following:
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 templates/template.html file and add the following:
And:
Restart the server for the changes to take effect:
And check out the result in your own browser: 127.0.0.1:8000/testing/.
This will make the example work, but we want you to choose DEBUG = False, because that is the best way to learn how to work with Django.
Choose Debug = False
For the rest of this tutorial, we will run with DEBUG = False, even in development, because that is the best way to learn how to work with Django.
Didn't Work?
That is right, the example still does not work.
You will have install a third-party library in order to handle static files.
There are many alternatives, we will show you how to use a Python library called WhiteNoise in the next chapter.
Module quiz
2 questionsWhich of the following is true about Add Static Files?
What is the most common pitfall when working with Add Static Files?
Answer all questions to submit.