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

Nac*_*cho 14 python django django-comments

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

在同一个doc页面上,它说要走的路是从BaseCommentAbstractModel扩展我的自定义注释类,但就是这样,我到目前为止,现在我不知所措.我在这个具体方面找不到任何东西.

mit*_*chf 12

我最近实现了Ofri提到的解决方案,因为我只想接受一个单独的"评论"字段来评论(比如SO,没有"名字",没有"电子邮件",没有"网址").

为了自定义默认注释表单和列表显示,我在根目录"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 %}
Run Code Online (Sandbox Code Playgroud)

这与默认评论表格略有不同,主要是抑制不需要的"姓名","电子邮件"和"网址"输入的显示.

我的"/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 %}
Run Code Online (Sandbox Code Playgroud)

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

这对我很有用,并且仍然给我所有其他"免费"的东西,附带django评论(适度,标记,提要,多态关联等...)