python - 如何在 django 中创建基本布局文件
问题描述
在templates
文件夹中我有 2 个 HTML 文件index.html
和add.html
文件,我想创建一个布局文件并想在其中添加页眉和页脚,在中间内容中我想将我的 2 个文件覆盖到该布局文件中,任何人都可以帮忙我用这个?
这是我的 2 个 html 文件:
索引.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
<table>
<tr>
<th>Page Title</th>
<th>Update Date</th>
<th>Action</th>
</tr>
{% if pagelist %}
{% for page in pagelist %}
<tr>
<td>{{ page.title }}</td>
<td>{{ page.updated_date }}</td>
<td><a href="{% url 'crud:update' page.id %}">Edit</a></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2">Np Pages are available</td>
</tr>
{% endif %}
</table>
添加.html:
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
<form method="post" action="{% url 'crud:add' %}" name="page_form" id="page_form">
<input type="text" name="title" value="{{ title }}">
<input type="text" name="permialink" value="{{ permialink }}">
<input type="text" name="updated_date" value="">
<input type="text" name="bodytext" value="">
<input type="hidden" name="pages_id" value="{{ page_id }}">
{% csrf_token %}
<input type="submit" name="submit" value="Submit">
</form>
解决方案
按照本教程进行操作,例如:
base.html
<body>
<div class="page-header">
<!-- your header here -->
</div>
<div class="content container">
<div class="row">
<div class="col-md-8">
{% block content %}
{% endblock %}
</div>
</div>
</div>
<div class="page-footer">
<!-- your footer here -->
</div>
</body>
然后index.html
可以扩展base.html
如下:
索引.html
{% extends 'base.html' %}
{% block content %}
<table>
<tr>
<th>Page Title</th>
<th>Update Date</th>
<th>Action</th>
</tr>
{% if pagelist %}
{% for page in pagelist %}
<tr>
<td>{{ page.title }}</td>
<td>{{ page.updated_date }}</td>
<td><a href="{% url 'crud:update' page.id %}">Edit</a></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2">Np Pages are available</td>
</tr>
{% endif %}
</table>
{% endblock %}
请注意,您可以添加更多块,这些块可以在base.html
模板中覆盖以渲染js
和css
内部头部等..)。
一旦掌握并且可以在子模板中覆盖它们blocks of code
的事实,这很容易
add.html
对模板做类似的事情
推荐阅读
- google-cloud-functions - 直接在云函数中处理静态文件,而不是从存储中处理
- java - 是否有更简洁的方法可以在处理中使用 Java 交替形状和背景颜色?
- python - 有没有办法将 .ipynb 或 .py 文件转换为 .do stata 文件?
- c++ - 指向超类对象的指针可以用作指向子类对象的指针。但不能调用子类的成员函数。为什么?
- html - 如何将一张图片显示在另一张图片上作为背景?
- python - scipy.interpolate.splprep 函数中的系数是如何得出的
- javascript - 在 Google 表格中,如何使用 GAS 将日期格式为“MMMM”(仅月份名称)的单元格格式化为大写?
- python - 在 python 中读取 Pandas 数据框中的浮点数时出现问题
- postgresql - 带有本地执行程序和 postgress 的 Airflow 2.0 调度程序失败
- haskell - 使用自动映射将 IO 异常提升到 MonadError