1

我创建了一个带有“parentId”和“email_notification”等自定义字段的评论应用程序。在模板中,comments标签可以拉出我添加的项目,但“get_comment_form”标签不显示表单。另外,如何自定义该表单中的字段?

模型.py

from django.db import models
from django.contrib.comments.models import Comment

class CommentWithParent(Comment):
    parentId = models.IntegerField(default = 0)
    email_notification = models.BooleanField(default = False)

表格.py

from django import forms
from django.contrib.comments.forms import CommentForm
from mblog.my_comment.models import CommentWithParent
from django.db import models


class CommentWithParentForm(CommentForm):

    parentId = models.IntegerField(default = 0)
    email_notification = models.BooleanField(default = False)

    def get_comment_model(self):
        return CommentWithParent

    def get_comment_create_data(self):
        data = super(CommentWithParentForm, self).get_comment_create_data()
        return data

    def get_form(self):
        return self

模板文件

{% load comments %}
{% get_comment_form for entry as form %}
4

1 回答 1

0

您可以在文档中了解有关自定义表单模板的更多信息。一般来说,您可以从表单中隐藏某些字段或控制使用哪些表单小部件来显示它们。要更改它们的外观,您需要使用应用程序的 CSS,或者按照文档描述创建一个新模板。

我不完全确定为什么get_comment_form标签不适合你。上面链接的文档建议您在视图中需要一个表单,然后模板可以使用不同的标签呈现该表单;这是文档的那一部分。如果您不使用 Django 1.4,情况可能会略有不同。

于 2012-09-20T13:29:00.843 回答