Django评论:想要删除用户URL,而不是扩展模型.如何?

ano*_*ard 13 django django-forms django-comments

我完全理解扩展Django中的评论应用程序的文档,并且真的想坚持使用自动功能但是 ......

在当前的应用程序中,我绝对没有使用"URL"与评论一起提交.

作为微创默认设置的,我怎么能阻止这一领域的显示与评论表单起来

使用Django 1或Trunk,以及尽可能多的通用/内置函数(通用视图,默认注释设置等等.到目前为止,我只有一个通用的视图包装器).

kil*_*ney 17

由于某些原因,我不能评论SmileyChris的帖子,所以我将在这里发布.但是,我只是使用SmileyChris的回应而遇到了错误.您还必须覆盖get_comment_create_data函数,因为CommentForm将查找您删除的那些Post键.所以这是删除三个字段后的代码.

class SlimCommentForm(CommentForm):
"""
A comment form which matches the default djanago.contrib.comments one, but with 3 removed fields
"""
def get_comment_create_data(self):
    # Use the data of the superclass, and remove extra fields
    return dict(
        content_type = ContentType.objects.get_for_model(self.target_object),
        object_pk    = force_unicode(self.target_object._get_pk_val()),
        comment      = self.cleaned_data["comment"],
        submit_date  = datetime.datetime.now(),
        site_id      = settings.SITE_ID,
        is_public    = True,
        is_removed   = False,
    )


SlimCommentForm.base_fields.pop('url')
SlimCommentForm.base_fields.pop('email')
SlimCommentForm.base_fields.pop('name')
Run Code Online (Sandbox Code Playgroud)

这是您要覆盖的功能

def get_comment_create_data(self):
    """
    Returns the dict of data to be used to create a comment. Subclasses in
    custom comment apps that override get_comment_model can override this
    method to add extra fields onto a custom comment model.
    """
    return dict(
        content_type = ContentType.objects.get_for_model(self.target_object),
        object_pk    = force_unicode(self.target_object._get_pk_val()),
        user_name    = self.cleaned_data["name"],
        user_email   = self.cleaned_data["email"],
        user_url     = self.cleaned_data["url"],
        comment      = self.cleaned_data["comment"],
        submit_date  = datetime.datetime.now(),
        site_id      = settings.SITE_ID,
        is_public    = True,
        is_removed   = False,
    )
Run Code Online (Sandbox Code Playgroud)

  • 你需要这些import语句:来自django.contrib.contenttypes.models从django.utils.encoding导入ContentType来自django.conf的import force_unicode导入设置import datetime (5认同)

Smi*_*ris 10

定制评论框架时,这有很好的文档记录.

您的所有应用都将使用get_form,返回CommentForm弹出的url字段的子类.就像是:

class NoURLCommentForm(CommentForm):
    """
    A comment form which matches the default djanago.contrib.comments one, but
    doesn't have a URL field.

    """
NoURLCommentForm.base_fields.pop('url')
Run Code Online (Sandbox Code Playgroud)

  • 我同意文档是伟大的,但我必须争辩说找到我的方式"base_fields"并使用.pop()并不是我容易遇到的.是的,评论框架已有详细记录,但"此"并非如此.我同意找到那种东西是我的责任,我非常感谢你的时间和帮助!多谢兄弟! (7认同)

use*_*625 5

我的快速而肮脏的解决方案:我将'email'和'url'字段隐藏在字段中,并使用任意值来摆脱'此字段是必需的'错误.

它不优雅,但它很快,我没有必须继承CommentForm.添加注释的所有工作都在模板中完成,这很好.它看起来像这样(警告:未经测试,因为它是我实际代码的简化版本):

{% get_comment_form for entry as form %}

<form action="{% comment_form_target %}" method="post"> {% csrf_token %}

{% for field in form %}

    {% if field.name != 'email' and field.name != 'url' %}
        <p> {{field.label}} {{field}} </p>
    {% endif %}

{% endfor %}

    <input type="hidden" name="email" value="foo@foo.foo" />
    <input type="hidden" name="url" value="http://www.foofoo.com" />

    <input type="hidden" name="next" value='{{BASE_URL}}thanks_for_your_comment/' />
    <input type="submit" name="post" class="submit-post" value="Post">
</form>
Run Code Online (Sandbox Code Playgroud)