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

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

问题描述

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



在同一个文档页面上它说,要从 BaseCommentAbstractModel 扩展我的自定义评论类,但是这几乎是这样,我已经到了现在,我已经失败了。我在这个具体方面找不到任何东西。

解决方案

我最近实施了Ofri所提到的解决方案,因为我只想接受一个单独的评论字段的评论(像SO,没有名字,没有电子邮件和没有网址)。



要自定义默认注释表单和列表显示,我在我的根templates目录中创建了一个comments目录,并覆盖了两个默认注释模板。 >

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

  { %load comment i18n%} 
{%if user.is_authenticated%}
< form action ={%comment_form_target%}method =post>
{%csrf_token%}
{%if next%}< input type =hiddenname =nextvalue ={{next}}/> {%endif%}
{%的形式为%}
{%if field.is_hidden%}
{{field}}
{%else%}
{%if field。 name!=name和field.name!=email和field.name!=url%}
{%if field.errors%} {{field.errors}} {%endif%}
< p {%if field.errors%} class =error{%endif%} {%ifequal field.namehoneypot%} style =display:none;{%endifequal%}> ;
{{field}}
< / p>
{%endif%}
{%endif%}
{%endfor%}
< input type =submitname =postclass =submit-post value ={%trans添加评论%}/>
< / form>
{%else%}
很抱歉,您必须是< a href =javascript:alert('send to login page')>登录< / a>提交评论。
{%endif%}

与默认评论表格略有不同,主要抑制不需要的名称,电子邮件和url输入的显示。



我的/templates/comments/list.html是: / p>

 < div class =comment_start>< / div> 
{%在comment_list%中发表评论}
< div class =comment>
{{comment.comment}}
(来自< a href =javascript:alert('show user profile / stats')> {{comment.user}}< / a> - {{comment.submit_date | timesince}}之前)
< / div>
{%endfor%}

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



这对我来说很好,还给我所有其他免费的东西,django评论(审讯,标记,饲料,多态关联等) ..)


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.

解决方案

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

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

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