如何覆盖Django contrib comments模板?
我正在使用的django评论表单是:
{% 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
这个模板的内容是:
{% 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网页项目上传到服务器后,我该如何编辑 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 %}
在本地电脑上,这次我直接修改了 django/contrib/comments/templates/comments/posted.html
的内容,让它硬编码继承我的项目 base.html
。
有没有人能给我一些解决这个问题的想法?我已经搜索了很多来解决这个问题。
1 个回答
10
只需要在你项目的“templates”目录里重写它就可以了:
<project_root>/templates/comments/posted.html
在评论应用或者Django的通用模板文档中似乎没有详细说明这个,但它的工作方式和重写管理模板是一样的(这个有文档说明)。