2012-01-30 34 views
0

Django的contrib請評價形成我使用:我如何覆蓋django contrib評論模板?

{% get_comment_form for post as form %} 
<form action="{% comment_form_target %}" method="post">{% csrf_token %} 
    {% if next %} 
     <div><input type="hidden" name="next" value="{{ next }}" /></div> 
    {% endif %} 
    {% for field in form %} 
     {% if field.is_hidden %} 
      <div>{{ field }}</div> 
     {% else %} 
      {% if field.name == 'comment' %} 
      {% if field.errors %}{{ field.errors }}{% endif %} 
      <p 
       {% if field.errors %} class="error"{% endif %} 
       {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}> 
       {{ field.label_tag }} {{ field }} 
      </p> 
      {% endif %}    
     {% endif %} 
    {% endfor %} 
    <p class="submit"> 
     <input type="submit" name="post" class="submit-post" value="{% trans "Post" %}" /> 
    </p> 
</form> 

提交表格後,它重定向到http://127.0.0.1:8000/comments/posted/?c=..

,這意味着它會調用模板django/contrib/comments/templates/comments/posted.html

django/contrib/comments/templates/comments/posted.html內容:

{% extends "comments/base.html" %} 
{% load i18n %} 

{% block title %}{% trans "Thanks for commenting" %}.{% endblock %} 

{% block content %} 
<h1>{% trans "Thank you for your comment" %}.</h1> 
{% endblock %} 

這不會擴展我的項目的base.html。

我需要定製/重寫該模板,以便擴展我的項目的base.html。我怎樣才能做到這一點?

如果我不能做到這一點,那麼如果我上傳我的Django的web項目在服務器上,那麼如何將我編輯django/contrib/comments/templates/comments/posted.html內容,使得它看起來像:

{% extends "path/to/myproject/templates/base.html" %} 
{% load i18n %} 

{% block title %}{% trans "Thanks for commenting" %}.{% endblock %} 

{% block content %} 
<h1>{% trans "Thank you for your comment" %}.</h1> 
{% endblock %} 

在本地PC,這次我更改/編輯django/contrib/comments/templates/comments/posted.html硬編碼的內容以擴展我的項目base.html

任何人都可以提供一些想法來解決這個問題嗎?我搜尋了很多來解決這個問題。

回答

8

只是覆蓋在你的項目的「模板」目錄:

<project_root>/templates/comments/posted.html 

它似乎並沒有在任何意見程序或Django的通用模板文件做到有據可查,但它的工作原理一樣overriding admin templates(這是記錄)。

+1

當我試圖向人們解釋並找不到支持文檔時,它總是令我煩惱:/事實上,這就是爲什麼我沒有回答! – 2012-01-30 21:08:51

+0

但是,我想它提到在管理文檔中有關每個應用程序/模型的自定義模板的位不適用於此。這是管理員的特殊情況。 – 2012-01-30 21:13:54