[PYTHON] Django Tutorial (Blog App Creation) ⑦ --Front End Complete

Last time, in Django Tutorial (Blog App Creation) ⑥ --Article Details / Edit / Delete Function, we created the details, edit, and delete screens for each article. ..

This time, we will make major adjustments to the template, but if we divide it roughly, we will do the following.

  1. Creating a screen common to all pages

  2. Creating a navigation bar

  3. Modify each template

  4. Delete unnecessary templates and processes

Creating a screen common to all pages

Not limited to Django, there are places on the homepage that are displayed in common even if the screen changes. In Qiita, the green navigation bar displayed at the top is a good example.

↓ This image.png However, it is difficult to write this in every template every time. If I write the code once and finish it, but when I think about the time when the correction was made ...

That's why we use ** common templates ** as a useful feature of Django. Simply put, the common parts are put together in one file, The different part for each screen is that you call and use a different template.

To do this, first create a file directly under the template folder. This time, let's create a file called /template/base.html.

└── templates
    ├── base.html #add to
    └── blog
        ├── index.html
        ├── post_confirm_delete.html
        ├── post_detail.html
        ├── post_form.html
        └── post_list.html

Write common processing in this file, and the parts that differ for each screen I will call a file such as post_detail.html.

Modify base.html

The contents will be like this.

base.html


<!doctype html>
<html lang="ja">
<head>
    <title>tmasuyama's blog</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <a class="navbar-brand" href="{% url 'blog:post_list' %}">Top</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="navbarSupportedContent">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="nav-link" href="{% url 'blog:post_create' %}">Post</a>
                </li>
            </ul>
        </div>
    </nav>

    <!--The contents of each template are called in this block-->
    {% block content %} #Attention!
    {% endblock %}      #Attention!
</div>

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

</body>
</html>

This tutorial doesn't go into detail about the front end, You can call and use Bootstrap from your CDN to make it look good with Bootstrap, The navigation bar is represented in