2017-08-14 57 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?

相關問題