2013-03-02 28 views
0

我有一個內聯表單集:Django的 - 在線表單集總是錯誤的POST

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

顯示formset.errors正確

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

謝謝。我這樣做,但好奇{&如果formset.errors%}是真的,但沒有顯示錯誤。它只是寫了請糾正錯誤: – 2013-03-03 14:40:11

+0

它好奇,我可以保存表格甚至與錯誤消息。在視圖中我使用formset.is_valid()並保存。 – 2013-03-03 14:49:11

+0

對不起,我將搜索爲什麼如果formset.errors返回True – catherine 2013-03-03 14:55:55