【问题标题】:Django html set upDjango html 设置
【发布时间】: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 与视图和模板耦合的文档,并根据需要发布代码。

标签: html django web


【解决方案1】:

像这样编辑/设计您的代码:

用于索引或 aboutme/header.html 的 HTML 内容

// codes here 

{% block blogcontent %}
    {% include 'path/blog.html' %}
{% enblock %}

// codes here 

{% block postcontent %}
    {% include 'path/post.html' %}
{% enblock %}


<!-- end  -->

当您拨打 127.0.0.1:8000 时,那里的一切都将可用。 对于其他链接,如果您不想显示博客或帖子块, 只需使用 {% block blogcontent %}{% endblock %} {% block postcontent %}{% endblock %} 里面没有任何东西

【讨论】:

  • 我知道怎么做....但是我不知道怎么做是让帖子显示....post.html 在数据库上绘制并且是另一个的一部分应用程序。将它移到这里,或者即使我将它留在它所在的应用程序中,帖子似乎也不会显示在这个引导页面中,除非我输入 127.0.0.1:8000/blog
  • 在你看来,你应该查询post表,并通过上下文发送给模板
  • 我使用过:{%include "blog/post.html"%} 并尝试访问以下路径中的 html 文件:C:\Users\User\Desktop\Django Tutorials\理解_html_setup\pythonsite\mysite\blog\templates\blog ///但它不起作用。我也试过 {%include "blog/blog/post.html"%} 和 {%include "blog/templates/blog/post.html"%} 但没有任何效果!
  • 哇!很多事情需要解决,但首先:告诉我或者在你的项目中本地化你的 post.html,这真的很重要,你设置了什么 settings.TEMPLATES_DIRS ?永远不要在你的项目中使用完整路径,让 django 和 python 来做。
猜你喜欢
  • 2012-06-14
  • 2018-08-07
  • 2014-10-07
  • 2021-06-23
  • 2016-07-31
  • 2019-03-06
  • 1970-01-01
  • 2010-10-19
  • 2017-04-12
相关资源
最近更新 更多