我如何覆盖 django contrib 评论模板?

发布于 2025-01-01 08:47:19 字数 2120 浏览 1 评论 0 原文

我正在使用的 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 %}

在本地电脑上,这一次我更改/编辑了 django/contrib/comments/templates/comments/posted.html 硬编码的内容以扩展我的项目base.html

谁能提供一些想法来解决这个问题?我已经搜索了很多来解决这个问题。

The django contrib comments form i'm using:

{% 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>

After submitting the form, it redirects to http://127.0.0.1:8000/comments/posted/?c=..

That means it calls the template django/contrib/comments/templates/comments/posted.html

The content of 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 %}

That doesn't extends my project's base.html.

I need to customize/override that template so that it extends my project's base.html. How can i do that?

If i can't do that, then if i upload my django web project on server, then how would i edit the content of django/contrib/comments/templates/comments/posted.html so that it looks like that:

{% 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 %}

On local pc, for this time i changed/edited the content of django/contrib/comments/templates/comments/posted.html hard-coded to extends my project base.html.

Can anyone give some idea to solve this? I have searched a lot to solve this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清风无影 2025-01-08 08:47:19

只需在项目的“templates”目录中覆盖它:

<project_root>/templates/comments/posted.html

它似乎没有在评论应用程序或 Django 的通用模板文档中得到很好的记录,但它的工作方式与 覆盖管理模板(已记录)。

Just override it in your project's "templates" directory:

<project_root>/templates/comments/posted.html

It doesn't seem to be well documented in either the comments app or Django's general template documentation, but it works the same as overriding admin templates (which is documented).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文