如何通过删除不必要的字段来扩展评论框架(django)?

发布于 2024-08-24 02:24:11 字数 419 浏览 5 评论 0原文

我一直在阅读有关评论框架以及如何自定义它的 django 文档(http://docs.djangoproject.com/en/1.1/ref/contrib/comments/custom/) 在该页面中,它展示了如何向表单添加新字段。但我想要做的是删除不必要的字段,例如 URL、电子邮件(以及其他次要 mods)。

在同一个文档页面上,它说要采取的方法是将我的自定义评论类从 < strong>BaseCommentAbstractModel,但仅此而已,我已经走了这么远,现在却不知所措。我在这个特定方面找不到任何内容。

I've been reading on the django docs about the comments framework and how to customize it (http://docs.djangoproject.com/en/1.1/ref/contrib/comments/custom/)
In that page, it shows how to add new fields to a form. But what I want to do is to remove unnecesary fields, like URL, email (amongst other minor mods.)

On that same doc page it says the way to go is to extend my custom comments class from BaseCommentAbstractModel, but that's pretty much it, I've come so far and now I'm at a loss. I couldn't find anything on this specific aspect.

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

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

发布评论

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

评论(3

灼疼热情 2024-08-31 02:24:11

我最近实现了 Ofri 提到的解决方案,因为我只想接受一个单独的“评论”字段作为评论(就像 SO 所做的那样,没有“名称”,没有“电子邮件”,也没有“url”)。

为了自定义默认的评论表单和列表显示,我在根“templates”目录中创建了一个“comments”目录,并覆盖了两个默认的评论模板。

我的“/templates/comments/form.html”是:

{% load comments i18n %}
{% if user.is_authenticated %}
    <form action="{% comment_form_target %}" method="post">
        {% csrf_token %}
        {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
        {% for field in form %}
            {% if field.is_hidden %}
                {{ field }}
            {% else %}
                {% if field.name != "name" and field.name != "email" and field.name != "url" %}
                    {% if field.errors %}{{ field.errors }}{% endif %}
                    <p {% if field.errors %} class="error"{% endif %} {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
                    {{ field }}
                    </p>
                {% endif %}
            {% endif %}
        {% endfor %}
        <input type="submit" name="post" class="submit-post" value="{% trans "Add Comment" %}" />
    </form>
{% else %}
    I'm sorry, but you must be <a href="javascript:alert('send to login page')">logged in</a> to submit comments.
{% endif %}

这与默认评论表单略有不同,主要是抑制不需要的“名称”、“电子邮件”和“网址”输入的显示。

我的“/templates/comments/list.html”是:

<div class="comment_start"></div>
{% for comment in comment_list %}
    <div class="comment">
       {{ comment.comment }} 
       (from <a href="javascript:alert('show user profile/stats')">{{ comment.user }}</a> - {{ comment.submit_date|timesince }} ago)
    </div>
{% endfor %}

在我想要表单的页面上,我首先调用 {% load comments %} 然后 {% render_comment_form for [object] %} 显示表单,或 {% render_comment_list for [object] %} 生成对象注释列表(将 [object] 替换为适当的对象名称)。

这对我来说非常有用,并且仍然为我提供了 django 评论附带的所有其他“免费”内容(审核、标记、提要、多态关联等......)

I recently implemented the solution that Ofri mentioned, since I only wanted to accept a solitary "comment" field for a comment (like SO does, no "name", no "email" and no "url").

To customize the default comment form and list display, I created a "comments" directory in my root "templates" directory and overrode the two default comment templates.

My "/templates/comments/form.html" is:

{% load comments i18n %}
{% if user.is_authenticated %}
    <form action="{% comment_form_target %}" method="post">
        {% csrf_token %}
        {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %}
        {% for field in form %}
            {% if field.is_hidden %}
                {{ field }}
            {% else %}
                {% if field.name != "name" and field.name != "email" and field.name != "url" %}
                    {% if field.errors %}{{ field.errors }}{% endif %}
                    <p {% if field.errors %} class="error"{% endif %} {% ifequal field.name "honeypot" %} style="display:none;"{% endifequal %}>
                    {{ field }}
                    </p>
                {% endif %}
            {% endif %}
        {% endfor %}
        <input type="submit" name="post" class="submit-post" value="{% trans "Add Comment" %}" />
    </form>
{% else %}
    I'm sorry, but you must be <a href="javascript:alert('send to login page')">logged in</a> to submit comments.
{% endif %}

Which is only slightly different from the default comments form, primarily suppressing the display of the not-required "name", "email" and "url" inputs.

My "/templates/comments/list.html" is:

<div class="comment_start"></div>
{% for comment in comment_list %}
    <div class="comment">
       {{ comment.comment }} 
       (from <a href="javascript:alert('show user profile/stats')">{{ comment.user }}</a> - {{ comment.submit_date|timesince }} ago)
    </div>
{% endfor %}

On the page I want the form, I first call {% load comments %} and then {% render_comment_form for [object] %} to show the form, or {% render_comment_list for [object] %} to generate a list of the comments on the object (replace [object] with your appropriate object name).

This is working great for me, and still giving me all the other "free" stuff that comes with django comments (moderation, flagging, feeds, polymorphic associations, etc...)

心的位置 2024-08-31 02:24:11

关于如何通过实际的注释框架子类化方法优雅地做到这一点的简洁总结,而不是将元素隐藏在表单/其他不整洁的黑客中,可以找到 Django 注释:想要删除用户 URL,而不是扩展模型。如何?

本质上,您对 CommentForm 进行子类化,并更改其 get_comment_create_data(self) 方法,然后弹出您不需要的属性(例如电子邮件、url 等)

J

A tidy summary of how to do this elegantly, through the actual comments framework subclassing approach, rather than hiding elements in a form/other untidy hacks, can be found Django Comments: Want to remove user URL, not expand the model. How to?

Essentially, you subclass the CommentForm, and change its get_comment_create_data(self) method, and then pop out the attributes you don't want (e.g. email, url, etc.)

J

尹雨沫 2024-08-31 02:24:11

您可以尝试 覆盖评论表单使用自定义模板,仅显示您想要的字段。

You can try overriding the comment form with a custom template that only shows the fields you want.

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