2017-08-14 69 views
0

对于一个项目,我们正在尝试构建一个基本的类似论坛的网站;然而,我们试图张贴在多个页面,而不是一个,不能添加其他扩展上,它允许后要添加到该页面的部分:如何使用django在多个页面上发布

{% extends 'blog/base.html' %} 
 

 
{% block content %} 
 
    <div class="post"> 
 
     {% if post.published_date %} 
 
      <div class="date"> 
 
       {{ post.published_date }} 
 
      </div> 
 
     {% endif %} 
 
     {% if user.is_authenticated %} 
 
    <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a> 
 
{% endif %} 
 
     <h1>{{ post.title }}</h1> 
 
     <p>{{ post.text|linebreaksbr }}</p> 
 
    </div> 
 
{% endblock %}

有什么办法使网站使用其他方法在多个页面上显示这些帖子?

回答

0

我想你问的是“包含”关键字?和“带”模板标签?

post_template.html

<div class="post"> 
     {% if post.published_date %} 
      <div class="date"> 
       {{ post.published_date }} 
      </div> 
     {% endif %} 
     {% if user.is_authenticated %} 
    <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a> 
{% endif %} 
     <h1>{{ post.title }}</h1> 
     <p>{{ post.text|linebreaksbr }}</p> 
    </div> 

some_page.html

{% extends "base.html" %} 
{% with some_post as post %}{% include "post_template.html"%} {% endwith %} 

other_page.html

{% extends "base.html" %} 
{% with some_other_post as post %}{% include "post_template.html"%} {% endwith %} 
+0

我会联系,而不是什么文件some_post占位符? – ctug

0

WHE逆向工程&你想要的职位显示,假设你传递称为posts类型的字典列表:

{% for post in posts %} 
    {% include 'templates/post.html' %} 
{% endfor %} 

templates/post.html

<div class="post"> 
    {% if post.published_date %} 
     <div class="date"> 
      {{ post.published_date }} 
     </div> 
    {% endif %} 
    {% if user.is_authenticated %} 
    <a class="btn btn-default" href="{% url 'post_edit' pk=post.pk %}"><span class="glyphicon glyphicon-pencil"></span></a> 
    {% endif %} 
    <h1>{{ post.title }}</h1> 
    <p>{{ post.text|linebreaksbr }}</p> 
</div> 

参见:How do you insert a template into another template?

相关问题