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)
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)
我的快速而肮脏的解决方案:我将'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)