如何通过删除不必要的字段来扩展评论框架(django)?
我一直在阅读有关评论框架以及如何自定义它的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近实现了 Ofri 提到的解决方案,因为我只想接受一个单独的“评论”字段作为评论(就像 SO 所做的那样,没有“名称”,没有“电子邮件”,也没有“url”)。
为了自定义默认的评论表单和列表显示,我在根“templates”目录中创建了一个“comments”目录,并覆盖了两个默认的评论模板。
我的“/templates/comments/form.html”是:
这与默认评论表单略有不同,主要是抑制不需要的“名称”、“电子邮件”和“网址”输入的显示。
我的“/templates/comments/list.html”是:
在我想要表单的页面上,我首先调用
{% 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:
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:
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 注释:想要删除用户 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
您可以尝试 覆盖评论表单使用自定义模板,仅显示您想要的字段。
You can try overriding the comment form with a custom template that only shows the fields you want.