【发布时间】:2018-05-30 00:26:53
【问题描述】:
我有一个使用 Django 设置的基本网站,并且一直在关注 sentdex 教程。我无法弄清楚如何在引导程序“关于”部分中包含博客文章(我假设这将是 blog.html 或 post.html),我已将其重命名为博客。基本上,我希望博客文章(来自数据库)出现在引导程序的“关于”部分,而不是在底部或单独的页面中。
我有一个包含 home.html
的“aboutme”应用(主网站){%extends "aboutme/header.html" %}
{%block content%}
<p>Welcome to this "about" page that is all about the website:
{%include "aboutme/includes/mysnippet.html"%}
{% endblock %}
{%block blogcontent%}
{% endblock %}
..和一个 header.html,其中包含引导站点本身的整个 index.html。下面是 bootstrap websit about/blog 部分的 about 部分,其中包括 django 模板逻辑(块内容)
<!-- About Section (Blog)-->
<section class="bg-primary text-white mb-0" id="about">
<div class="container">
<h2 class="text-center text-uppercase text-white">Blog</h2>
<hr class="star-light mb-5">
<div class="row">
<div class="col-lg-4 ml-auto">
<p class="lead">This is the blog section</p>
{%block blogcontent%}
{%endblock %}
</div>
<div class="col-lg-4 mr-auto">
<p class="lead">The blogs outlined in the models section and stored in the database, are going to be displayed here.</p>
</div>
</div>
<div class="text-center mt-4">
<a class="btn btn-xl btn-outline-light" href="#">
<i class="fa fa-download mr-2"></i>
Download Now!
</a>
</div>
</div>
</section>
最后,我有另一个名为“博客”的应用程序,在该应用程序的模板/博客目录中: post.html 和 blog.html
blog.html
{% extends "aboutme/header.html" %}
{%block content %}
{% for post in object_list %}
<h5>{{post.date|date:"Y-m-d"}}<a href="/blog/{{post.id}}">{{post.title}}</a></h5>
{% endfor %}
{% endblock %}
post.html
{% extends "aboutme/header.html" %}
{%block content %}
<h3>{{post.title}}</h3>
<h6>on{{post.date}}</h6>
{{post.body|safe|linebreaks}}
{% endblock %}
在我输入http://127.0.0.1:8000/blog 的那一刻......博客文章出现在页面底部,或者我放置 {%block content %}{% endblock %} 的任何地方(但它似乎只适用于页面底部)。
如何让博文出现在引导网站的“关于我”部分?
我也很想解释一下 home.html 页面到底是做什么的,它可以被命名为任何东西还是必须是“家”。是否根据显示的内容定义了所有内容,以及它是如何工作的?
【问题讨论】:
-
哪个模板被视为基础?那个模板: {% extends "aboutme/header.html" %} ?因为我看不到你把 {% block content %}{% endblock %} 放在哪里
-
aboutme 应用程序和 header.html 被视为索引或基础 ..
-
基本上,我想知道我是如何得到 put post.html 或 blog.html 的(所以利用为帖子存储的数据库值)并将它们动态地放入 header.html 中。目前我必须去 127.0.0.1:8000/blog 让博客帖子出现在主网站上,但看不到如何让它们出现在主网站上......
-
而不是在 post/blog html 页面中使用扩展,只需在 index.html 模板中使用 include : ({% include 'path/post.html' %} 或 {% include 'path/ blog.html' %}) 和里面,不要使用块。确保通过索引调用根 url 中的所有帖子/博客上下文
-
模板可以任意命名。说“我输入127.0.0.1:8000/blog”并不能说明您正在使用什么视图和网址。您可能需要考虑阅读有关如何将 url 与视图和模板耦合的文档,并根据需要发布代码。