如何通过删除不必要的字段来扩展注释框架(django)? [英] How to extend the comments framework (django) by removing unnecessary fields?

查看:90
本文介绍了如何通过删除不必要的字段来扩展注释框架(django)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.)

在同一文档页面上,它说要走的路是从 BaseCommentAbstractModel 扩展我的自定义注释类,但实际上,我已经走了很远,现在我正在失利.在这个特定方面,我找不到任何东西.

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.

推荐答案

我最近实现了Ofri提到的解决方案,因为我只想接受一个单独的"comment"字段进行评论(例如SO一样,没有"name" ,没有电子邮件",也没有"URL").

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").

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

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.

我的"/templates/comments/form.html"是:

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

与默认注释表单仅稍有不同,主要是禁止显示不需要的名称",电子邮件"和"URL"输入.

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

我的"/templates/comments/list.html"是:

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

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

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).

这对我来说非常有用,并且仍然给我Django注释附带的所有其他免费"内容(审核,标记,提要,多态关联等)

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...)

这篇关于如何通过删除不必要的字段来扩展注释框架(django)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆