Django - 内联formset在POST上总是有错误

时间:2013-03-02 21:57:28

标签: django django-forms

我有一个内联formset:

OptionFormset = inlineformset_factory(App, AppOptions, can_delete=True, extra=1,
            fields = ['options', 'conditions'])

提交表单时,我会在模板中进行验证:

      {% if formset.errors %}
          <p style="color: red;">
              Please correct the error:
          {{ formset.errors }}
          </p>
      {% endif %}

打印返回:

Please correct the error [{}, {}] 

显然,表单中没有错误,并且formset.errors中没有错误错误可能在视图中?

view.py:

  def setAppOption(request, app_name):
      app_selected = App.objects.get(app_name=app_name, created_by=request.user)
      formset = OptionsFormSet(instance=fusionTable_selected)
      if request.method == 'POST':
          formset = OptionFormset(request.POST, instance=fusionTable_selected)
            if formset.is_valid():   
              formset.save()

      return render_to_response("ezApp/manage__Options.html", {'formset': formset}, context_instance=RequestContext(request))

models.py

    class App(models.Model):
        layer_name = models.SlugField(max_length=50)

        def __unicode__(self):
            return self.layer_name

    class AppOptions(models.Model):
        app = models.ForeignKey(EzApp)
        condition = models.CharField('SQL Query Conditions', max_length=100, blank=True)
        option = models.CharField('SQL Query Conditions', max_length=100, blank=True

1 个答案:

答案 0 :(得分:1)

正确显示formset.errors

{% for dict in formset.errors %}
    {% for error in dict.values %}           
        Please correct the error: {{ error }}
    {% endfor %}
{% endfor %}