[PYTHON] Get started with Django! ~ Tutorial ⑥ ~

Overview

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.

Static file name

In Django, static files are named ** static **. (Other frameworks include public and www.)

Location of static files

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` ``.

Creating a style

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.

localhost_8000_polls_.png

The text is green, reflecting style.css.

Summary

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.

GitHub

series

-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

Get started with Django! ~ Tutorial ⑤ ~
Get started with Django! ~ Tutorial ④ ~
Get started with Django! ~ Tutorial ⑥ ~
How to get started with Django
Django 1.11 started with Python3.6
Step notes to get started with django
Get started with MicroPython
Get started with Mezzanine
Getting Started with Django 2
The easiest way to get started with Django
Get started with influxDB + Grafana
Getting Started with Python Django (1)
Getting Started with Python Django (4)
Getting Started with Python Django (3)
Getting Started with Python Django (6)
Get started with Python! ~ ② Grammar ~
Getting Started with Django with PyCharm
Getting Started with Python Django (5)
Get started with Python! ~ ① Environment construction ~
Link to get started with python
Get started with MicroPython (on macOS)
How to get started with Scrapy
How to get started with Python
Get started with machine learning with SageMaker
Get started with Python in Blender
Get started with the Python framework Django on Mac OS X
Python Django Tutorial (5)
Python Django Tutorial (2)
I tried to get started with Hy
CRUD GET with Nuxt & Django REST Framework ②
Internationalization with django
django tutorial memo
Here's a brief summary of how to get started with Django
Python Django Tutorial (8)
CRUD GET with Nuxt & Django REST Framework ①
Python Django Tutorial (6)
Start Django Tutorial 1
Get started with the documentation tool Sphinx
Get Started with TopCoder in Python (2020 Edition)
CRUD with Django
Python Django Tutorial (7)
Python Django Tutorial (1)
Python Django tutorial tutorial
How Python beginners get started with Python with Progete
How to get started with laravel (Linux)
Python Django Tutorial (3)
[Blender x Python] Let's get started with Blender Python !!
Python Django Tutorial (4)
Python hand play (let's get started with AtCoder?)
Django Getting Started Part 2 with eclipse Plugin (PyDev)
Get started with Python on macOS Big Sur
A layman wants to get started with Python
[Cloud102] # 1 Get Started with Python (Part 1 Python First Steps)
Authenticate Google with Django
Upload files with Django
Development digest with Django
Get started Jupyter Notebook
Output PDF with Django
Getting started with Android!
Markdown output with Django
1.1 Getting Started with Python