Django评论框架,设置默认表单值

时间:2011-09-21 05:12:35

标签: django forms comments

我在forms.py中将自定义注释表单定义为fallow

class CommentFormWithReply(CommentForm):
    reply_to = forms.ModelChoiceField(queryset=CommentWithReply.objects.all(),
            widget=forms.HiddenInput(), required=False)

    def get_comment_model(self):
        # Use our custom comment model instead of the built-in one.
        return CommentWithReply

    def get_comment_create_data(self):
        # Use the data of the superclass, and add in the title field
        data = super(CommentFormWithReply, self).get_comment_create_data()
        return data

如何使用当前用户信息作为默认值(名称,电子邮件,网页)呈现此表单。

1 个答案:

答案 0 :(得分:0)

可能是这样的:

https://docs.djangoproject.com/en/dev/ref/forms/fields/#initial

if request.method == 'POST':
    form = CommentFormWithReply(request.POST)
    .........................................


if request.method == 'GET':
    default_data = {'name': 'Alexey', 'email': 'smt@email.smt', 'webpage': 'http://example.com'}
    form = CommentFormWithReply(default_data)