This is a continuation of Last time.
I've covered creating web applications in Django five times, but this is the final chapter.
In the final chapter, I would like to explain the handling of static files.
In Django, static files are named ** static **. (Other frameworks include public and www.)
It is the same as when placing the templates file, and the placement location is a little strange.
First, create a static directory inside the polls directory. Django looks for static files from there. Similar to how Django looks for templates in polls / templates /.
Therefore, the location will be ``` polls / static / polls / static file` ``.
Let's write CSS to check if the static file is actually reflected.
polls/static/polls/style.css
li a {
color: green;
}
Then modify the template file.
polls/templates/polls/index.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<li><a href="{% url 'polls:detail' question.id %}">{{question.question_text}}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No polls are available.</p>
{% endif %}
What we're doing is simple: first load the static template tag. The {% static%} template tag will generate the full URL of the static file.
Once you have done this, start the server and check.
The text is green, reflecting style.css.
I've explained Django's tutorials in all six times. The image of Python seemed to be strong in the field of mathematics such as machine learning, IoT and data analysis, but I found that I could create a web application firmly.
If I have a chance, I would like to use it in a project.
-Get started with Django! ~ Tutorial ① ~ -Get started with Django! ~ Tutorial ② ~ -Get started with Django! ~ Tutorial ③ ~ -Get started with Django! ~ Tutorial ④ ~ -Get started with Django! ~ Tutorial ⑤ ~ -Get started with Django! ~ Tutorial ⑥ ~
Recommended Posts