0

我正在为会员模型(模型 auth.User 会员资格到 app.Project)开发模型表单集。这里是 app/forms.py,在我删减了一些我在注释掉之后确认不相关的东西之后:

class EditMembership(forms.ModelForm):
  delete = forms.BooleanField(required=False, initial=False)

  class Meta:
    model = models.Membership
    fields = ('is_owner',)  

  def clean_delete(self):
    return self.cleaned_data['delete']

class BaseEditMemberships(forms.models.BaseModelFormSet):
  # Some custom methods not dealing with validation.
  pass

EditMembershipSet = forms.models.modelformset_factory(models.Membership,
  form=EditMembership, extra=0, formset=BaseEditMemberships)

这是我的模板:

<form method="{{ method|default:"post" }}" action="{{ action }}">
  {% csrf_token %}
  <ul>
  {% for form in formset %}
    <li>{{ form.instance.user}}</li>
    {{ form.as_ul }}
  {% endfor %}
  </ul>
  <input type="submit" value="{{ submit_value|default:"Submit" }}" />
</form>

而我的观点:

@ownership_required(message="You must be an owner to edit memberships.")
def edit_memberships(request, pk):
  project = get_object_or_404(models.Project, pk=pk)
  if request.method == "GET":
    formset = forms.EditMembershipSet(
      queryset=project.membership_set.all()
    )
    return render_formset_fullpage(request, formset, "Edit Memberships",
      template=forms.EDITMEMBERSHIPS_PATH)
  elif request.method == "POST":
    formset = forms.EditMembershipSet(request.POST)
    return HttpResponse("Hooray!")

和有问题的模型:

class Membership(models.Model):
  class Meta:
    unique_together = ('project', 'user')

  project = models.ForeignKey('common.Project', editable=False)
  user = models.ForeignKey('auth.User')
  is_owner = models.BooleanField(default=False, blank=False,
    help_text="Indicates ownership of the project")
  project_alias = models.CharField(max_length=CHARFIELD_SHORT,
    help_text="User-given name", blank=True)

问题是我在执行 POST 后不断收到 ValidationError:

ValidationError at /project/4/memberships
[u'ManagementForm data is missing or has been tampered with']

据我所知,forms.EditMembership 运行良好。我可以渲染该表单并从 POST 重新创建它而没有验证错误,所以我猜我使用 BaseEditMemberships/EditMembershipSet 的方式有问题。

编辑:

只是为了给您更多信息,这里有一个生成的示例表单和我的一个测试请求的 POST 变量。表格:

<form method="post" action="">
  <div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='d6d535efd35e83076e1ac14a82ef4cdc' /></div>
  <ul> 
    <li>someuser</li>
    <li><label for="id_form-0-is_owner">Is owner:</label>
      <input checked="checked" type="checkbox" name="form-0-is_owner" id="id_form-0-is_owner" />
    </li>
<li><label for="id_form-0-delete">Delete:</label>
  <input type="checkbox" name="form-0-delete" id="id_form-0-delete" />
  <input type="hidden" name="form-0-id" value="8" id="id_form-0-id" />
</li>

  </ul>
  <input type="submit" value="Submit" />
</form>

如果我选中删除框,则对应的 POST 变量:

  • form-0-id: u'8'
  • csrfmiddlewaretoken: u'd6d535efd35e83076e1ac14a82ef4cdc'
  • form-0-delete: u'on'
  • form-0-is_owner: u'on'
4

1 回答 1

2

您需要包含{{ formset.management_form }}在您的模板中。请参阅Django 的有关为表单集呈现模板的表单集文档

于 2013-02-21T18:18:14.663 回答