Django入门与实践-第12章:复用模板(完结)

Posted larken

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django入门与实践-第12章:复用模板(完结)相关的知识,希望对你有一定的参考价值。

<!--static/css/app.css-->
.navbar-brand {
    font-family: ‘Peralta‘, cursive;
}
<!--templates/base.html-->
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{% block title %}Django Boards{% endblock %}</title>
        <link rel="stylesheet" href="{% static ‘css/bootstrap.min.css‘ %}">
    </head>
    <body>
        <div class="container">
            <ol class="breadcrumb my-4">
                {% block breadcrumb %}
                {% endblock %}
            </ol>
          {% block content %}
          {% endblock %}
        </div>
    </body>
</html>


<!--现在我们有了 bast.html 模板,我们可以很轻松地在顶部添加一个菜单块:---->
<!--templates/base.html-->
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{% block title %}Django Boards{% endblock %}</title>
        <link rel="stylesheet" href="{% static ‘css/bootstrap.min.css‘ %}">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <div class="container">
                <a class="navbar-brand" href="{% url ‘home‘ %}">Django Boards</a>
            </div>
        </nav>
        <div class="container">
            <ol class="breadcrumb my-4">
              {% block breadcrumb %}
              {% endblock %}
            </ol>
            {% block content %}
            {% endblock %}
        </div>
    </body>
</html>


<!--在 bast.html 模板中添加这个字体:-->
{% load static %}<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>{% block title %}Django Boards{% endblock %}</title>
        <link href="https://fonts.googleapis.com/css?family=Peralta" rel="stylesheet">
        <link rel="stylesheet" href="{% static ‘css/bootstrap.min.css‘ %}">
        <link rel="stylesheet" href="{% static ‘css/app.css‘ %}">
    </head>
    <body>
        <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
            <div class="container">
              <a class="navbar-brand" href="{% url ‘home‘ %}">Django Boards</a>
            </div>
        </nav>
        <div class="container">
            <ol class="breadcrumb my-4">
              {% block breadcrumb %}
              {% endblock %}
            </ol>
            {% block content %}
            {% endblock %}
        </div>
    </body>
</html>
<!--templates/home.html-->
{% extends ‘base.html‘ %}
{% block breadcrumb %}
    <li class="breadcrumb-item active">Boards</li>
{% endblock %}

{% block content %}
    <table class="table">
        <thead class="thead-inverse">
            <tr>
                <th>Board</th>
                <th>Posts</th>
                <th>Topics</th>
                <th>Last Post</th>
            </tr>
        </thead>
        <tbody>
          {% for board in boards %}
            <tr>
                <td>
                  <a href="{% url ‘board_topics‘ board.pk %}">{{ board.name }}</a>
                  <small class="text-muted d-block">{{ board.description }}</small>
                </td>
                <td class="align-middle">0</td>
                <td class="align-middle">0</td>
                <td></td>
            </tr>
          {% endfor %}
        </tbody>
    </table>
{% endblock %}
<!--templates/topics.html-->
{% extends ‘base.html‘ %}

{% block title %}
    {{ board.name }} - {{ block.super }}
{% endblock %}

{% block breadcrumb %}
    <li class="breadcrumb-item"><a href="{% url ‘home‘ %}">Boards</a></li>
    <li class="breadcrumb-item active">{{ board.name }}</li>
{% endblock %}
{% block content %}
    <!-- just leaving it empty for now. we will add core heresoon. -->
{% endblock %}

 

以上是关于Django入门与实践-第12章:复用模板(完结)的主要内容,如果未能解决你的问题,请参考以下文章

Django入门与实践-第19章:主题回复(完结)

Django入门与实践-第20章:QuerySets(查询结果集)(完结)

Django入门与实践 17-26章总结

Django入门与实践-第22章:基于类的视图

Django入门与实践 1-16章总结

JavaScript设计模式与开发实践