Pyt*_*tor 2 python django templates
我的 Django 应用程序中有大约 12 个不同的 HTML 页面。我正在寻找一种方法来在 1 个模板文件中制作我的标题,然后将该文件添加到我需要标题的每个 HTML 模板中。
我尝试遵循 Django 的模板文档,但它对我来说不够简洁。有人能帮我把它分解得更进一步吗?
我基本上是从第 1 个方块开始的……我当时可以加载一个标题,但没有附加任何 css。我已经删除了代码,因为我不想搞砸任何事情。
有两种可能的方法来做到这一点。
1. 制作一个外部文件并将所有代码插入到内部文件中。这使用扩展。
外部文件 base.html
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
</head>
<style>
body { padding-top: 70px;
font-family: Poppins;
}
</style>
<body>
<navbar> This is your Navbar. Navbar does assume the use of Bootstrap</navbar>
<p> You can also put anything else you want on every page here.</p>
<main role="main" class="container">
{% block content %}
{% endblock %}
</main>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
内部文件 page.html
{% extends 'base.html' %}
{% block content %}
<p>This is the stuff you want to be unique to the page. </p>
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
这种方法的缺点是您实际上是将一个页面放在另一个页面中,并且灵活性稍差。
2. 在 HTML 文件中包含代码块。这用途包括。
要添加标题的页面,page.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
</head>
<style>
body { padding-top: 70px;
font-family: Poppins;
}
</style>
<body>
{% include "header.html" %}
<main role="main" class="container">
<p> Whatever you want on your page</p>
</main>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
标题的实际代码,header.html
<div class="header">
<p>My supercool header</p>
</div>
Run Code Online (Sandbox Code Playgroud)
Include 将在您放置 include 语句的位置插入该 HTML 代码块。
| 归档时间: |
|
| 查看次数: |
3395 次 |
| 最近记录: |