繁体   English   中英

Django评论:无法设置内容类型

[英]Django-reviews: cannot set content-type

我正在使用django-reviews(http://code.google.com/p/django-reviews/),并且无法设置评论的内容类型。 简单的例子:

def check_review(request): 
    if request.method == 'POST': 
        reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk']) 
        review_form = ReviewForm(target_object=reviewed_item, data=request.POST) 
        review_form.content_type = ContentType.objects.get_for_model(MyModel) 

但是,表单的“ content_type”字段没有值,并且表单具有错误“((隐藏字段content_type)此字段是必需的。”)。 我尝试过多种方法来设置content_type,但是没有运气。 有任何想法吗?

为了澄清,我假设您看到显示的表单,将其填写在值中并尝试提交。 此时,您会看到表单验证错误,指示您需要填写隐藏字段。对吗?

如果是这样,则需要在提交之前在表单上设置content_type。 通常我会执行以下操作:

def check_review(request):
    if request.method == 'POST':
        reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk'])
        review_form = ReviewForm(target_object=reviewed_item, data=request.POST)
        if review_form.is_valid():
            # do some processing here
    else:
        # We're just getting an unbound form
        reviewed_item = get_object_or_404(MyModel, pk=request.POST['object_pk'])
        review_form = ReviewForm(target_object=reviewed_item, data=request.POST)
        review_form.content_type = ContentType.objects.get_for_model(MyModel)
    # return with review_form in the template's context or what have you

我发现您可以使用

review_form.base_fields["content_type"] = ...

虽然这似乎仍然不适用于content_type。 这实际上是我的错误。 我的意思是在验证表单后实例化Review对象,并在该对象上设置content_type。 容易得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM