2012-02-09 57 views
0

[編輯:請參閱下面的回答 - 此問題的起源是無效的標記,並且瀏覽器非常努力地隱藏該標記。 ]Django formset顯然只在模板中打印第一個表格(因爲無效標記)

我有一個formset,其中絕對是應該包含兩種形式,但無論出於何種原因,我只能得到一個打印在模板中的表單。

這是模板行:

<tr id="existing_docs_row"><td colspan="2">{{ existing_articles.management_form }}{% for f in existing_articles %}<div>{{ f }}</div>{% endfor %}</td></tr> 

我得到完全相同的行爲(少div標籤)有:

<tr id="existing_docs_row"><td colspan="2">{{ existing_articles }}}</td></tr> 

的管理形式和第一種形式的創建,而不是第二。這就是我得到了我的瀏覽器:

<input type="hidden" id="id_form-TOTAL_FORMS" value="2" name="form-TOTAL_FORMS"><input type="hidden" id="id_form-INITIAL_FORMS" value="2" name="form-INITIAL_FORMS"><input type="hidden" id="id_form-MAX_NUM_FORMS" name="form-MAX_NUM_FORMS"><div><div class="selected_row " id="selected_row"><span class="formlabel"></span><ul> 
<li><label for="id_form-0-selected_0"><input type="radio" name="form-0-selected" value="True" id="id_form-0-selected_0"> </label></li> 
</ul></div> 
<div class="original_filename_row " id="original_filename_row"><span class="formlabel"><span id="for-id_form-0-original_filename-">Original filename:</span></span><div id="id_form-0-original_filename" name="form-0-original_filename">FakeExampleCompanyName.docx</div></div> 
<div class="tags_row " id="tags_row"><span class="formlabel"><span id="for-id_form-0-tags-">Tags:</span></span><div id="id_form-0-tags" name="form-0-tags" class="tagarea"><span class="tagitem">England and Wales</span> <span class="tagitem">Private company limited by shares</span> <span class="tagitem">Model articles with amendments</span></div></div> 

在視圖打破,並打印表單集表明,它包含兩種形式(existing_template_formset是視圖裏面的表單集的名稱):

>>> print existing_template_formset <input type="hidden" name="form-TOTAL_FORMS" value="2" id="id_form-TOTAL_FORMS" /><input type="hidden" name="form-INITIAL_FORMS" value="2" id="id_form-INITIAL_FORMS" /><input type="hidden" name="form-MAX_NUM_FORMS" id="id_form-MAX_NUM_FORMS" /> 
<div id="selected_row" class="selected_row "> <span class="formlabel"></span><ul> <li><label for="id_form-0-selected_0"><input type="radio" id="id_form-0-selected_0" value="True" name="form-0-selected" /> </label></li> </ul></div> <div id="original_filename_row" class="original_filename_row "><span class="formlabel"><span id="for-id_form-0-original_filename-">Original filename:</span></span><div name="form-0-original_filename" id="id_form-0-original_filename">FakeExampleCompanyName.docx</div></div> <div id="tags_row" class="tags_row "><span class="formlabel"><span id="for-id_form-0-tags-">Tags:</span></span><div class="tagarea" name="form-0-tags" id="id_form-0-tags" ><span class="tagitem" >England and Wales</span> <span class="tagitem" >Private company limited by shares</span> <span class="tagitem" >Model articles with amendments</span></div></div> <tr><th></th><td><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr> 
<div id="selected_row" class="selected_row "><span class="formlabel"></span><ul> <li><label for="id_form-1-selected_0"><input type="radio" id="id_form-1-selected_0" value="True" name="form-1-selected" /> </label></li> </ul></div> <div id="original_filename_row" class="original_filename_row "><span class="formlabel"><span id="for-id_form-1-original_filename-">Original filename:</span></span><div name="form-1-original_filename" id="id_form-1-original_filename" >FakeExampleCompanyName.docx</div></div> <div id="tags_row" class="tags_row "><span class="formlabel"><span id="for-id_form-1-tags-">Tags:</span></span><div class="tagarea" name="form-1-tags" id="id_form-1-tags" ></div></div> <tr><th></th><td><input type="hidden" name="form-1-id" id="id_form-1-id" /></td></tr> 

>>> len(existing_template_formset) 2 

如您所見,在這兩種情況下,formset中的表單總數都是2(如管理表單中所證明的),但第二個表單根本不會生成。

有沒有人遇到過這個?我該如何解決?

我在windows上使用python 2.7.2上的django 1.3.1。

爲了完整起見,在此可以創建該formset的代碼:

class ExistingTemplateFormset(modelformset_factory(ArticlesTemplate, extra = 0, form=ExistingTemplateForm)): 
    def __init__(self, *args, **kwargs): 
     super(ExistingTemplateFormset, self).__init__(*args, **kwargs) 
     for x in self: 
      x.fields['id'].widget = forms.HiddenInput() 

      x.fields['original_filename'].editable = False 
      x.fields['original_filename'].widget = SpanWidget(tag = u'div') 


      x.fields['tags'].widget= TagArea() 
      x.fields['tags'].help_text = u'' 

TagAreaSpanWidget存在)

在視圖:

existing_template_formset = ExistingTemplateFormset(queryset = the_organisation.get_template_articles()) 
+1

你在哪裏定義'existing_articles',它和'exisiting_template_formset'是一樣的嗎? – 2012-02-09 17:57:19

+0

@j_syk:「('existing_template_formset'是視圖中formset的名稱):」 – Marcin 2012-02-09 18:04:27

+0

你可以試試'{{myformset。as_p}}或'{for myformset%中的表單%} {{form.as_p}} {%endfor%}'? – 2012-02-09 18:15:15

回答

1

眼尖的讀者(其,事實證明,不包括我,因此這個問題)將注意到我的輸出包括在每個表單的末尾:

`<tr><th></th><td><input type="hidden" name="form-0-id" id="id_form-0-id" /></td></tr>` 

現在,當被代入<tr id="existing_docs_row"><td colspan="2">{{ existing_articles.management_form }}{% for f in existing_articles %}<div>{{ f }}</div>{% endfor %}</td></tr>導致無效的標記(一tr一個tr內!)。

所以,事實證明,模板產生的第二種形式,但瀏覽器的錯誤恢復的方法(在Chrome中,忽略了很多無效的標記;在Firefox中,浮動第二種形式在別處在DOM )創建了第二個表單沒有被生成的外觀。

總結:只是檢查DOM會誤導我。嘗試強制瀏覽器阻止錯誤,並查看原始標記。

相關問題