Django:在表单模板中更改<select>选项时丢失表单属性

时间:2017-11-14 15:36:54

标签: jquery django forms

在我的Django应用程序中,一个模板,用于手动显示模型中的表单字段。在这个模板中,我有几个字段,并且我的forms.py中设置了选项。其中一个选项是元组(&#39; new&#39;,&#39; New ...&#39;),当它被选中时,会出现一个弹出窗口(Bootstrap Modal),用户可以添加一个选项,添加到选择的选项。 但是当表单被提交时,我有一个错误,因为缺少此字段的表单键:KeyError:&#39; country&#39;,如果我打印表单,则不存在国家/地区密钥: {&#39;基因型&#39;:&#39;?&#39;,&#39; complete_genome&#39;:False,&#39; seq_tech&#39;:&#39;?&#39;, &#39; isolation_source&#39;:&#39;&#39; mol_type&#39;:&#39;?&#39;,&#39; NT_seq&#39;:&#39; TTG&#39;,&#39;来源&#39;:&#39;?&#39;,&#39;主持&#39;:&#39;鸡&#39;,&#39;物种&#39; :&#39; A&#39;,&#39;隔离&#39;:&#39; te1&#39;,&#39; sourceLink&#39;:&#39;&#39;,&#39; subtyping_ref&#39;:&#39;?&#39;,&#39; PK_idSequence&#39;:&#39; test&#39;,&#39; strain&#39;:&#39; te1_strain&#39; ,&#39;评论&#39;:&#39;&#39;,&#39;子类型&#39;:&#39;?&#39;} 我的forms.py: UNKNOWN_START_LIST = [(&#39;?&#39;,&#39;?&#39;)] NEW_END_LIST = [(&#39; new&#39;,&#39; New ...&#39;)] class SequenceForm(ModelForm):     #创建表单字段     PK_idSequence = CharField(max_length = 15,label =&#39; Sequence ID&#39;)     #获取数据库中的现有国家/地区值     tmpCountries = sorted(list(set([seq.country for seq in Sequence.objects.all()如果seq.country不是None和seq.country!=&#39;?&#39;])))     countryChoices = UNKNOWN_START_LIST + [(tmpCountry,tmpCountry)for tmpCountry in tmpCountries] + NEW_END_LIST     #the countryChoices将是[(&#39;?&#39;,&#39;?&#39;),(&#39; France&#39;,&#39; France&#39;),(& #39;德国&#39;德国&#39;),(&#39;日本&#39;日本&#39;),(&#39;美国&#39;,&# 39; USA&#39;),(&#39; new&#39;,&#39; New ...&#39;)]     country = ChoiceField(choices = countryChoices,                     初始=#39&;&#39 ;,                     required = False)     #...更多的东西     类Meta:         model =序列         fields = [&#39; PK_idSequence&#39;,&#39;基因型&#39;,&#39;子类型&#39;,&#39;主机&#39;,&#39;国家&#39;,&# 39; complete_genome&#39;,&#39; seq_tech&#39;,&#39; NT_seq&#39;,&#39; col_date&#39;,                 &#39; isolation_source&#39;,&#39;隔离&#39;,&#39;菌种&#39;,&#39;种类&#39;,&#39; mol_type&#39;,&#39; subtyping_ref& #39;,&#39;来源&#39;,&#39; sourceLink&#39;,&#39;评论&#39;]     #signal清洁表单     def clean(self):         cleaning_data = super(SequenceForm,self).clean()         #引发错误的地方         如果cleaning_data [&#39; country&#39;]不是None:             cleaning_data [&#39; country&#39;] = cleaning_data.get(&#39; country&#39;)。title() 我的template.html: {#记录新国家/地区的模式窗口#} &lt; div id =&#34; modal_new_country&#34;类=&#34;模态&#34;的tabindex =&#34; -1&#34;角色=&#34;对话框&#34;&GT;     &lt; div class =&#34; modal-dialog&#34;角色=&#34;文件&#34;&GT;         &lt; div class =&#34; modal-content&#34;&gt;                 &lt; div class =&#34; modal-header&#34;&gt;                     &lt; h5 class =&#34; modal-title&#34;&gt;记录新的国家/地区:&lt; / h5&gt;                 &LT; / DIV&GT;                 &lt; div class =&#34; modal-body&#34;&gt;                     &lt; input type =&#34; text&#34;名称=&#34; new_country_text&#34; ID =&#34; new_country_text&#34;&GT;                 &LT; / DIV&GT;                 &lt; div class =&#34; modal-footer&#34;&gt;                     &lt; button id =&#34; new_country_button&#34;类型=&#34;按钮&#34; class =&#34; btn btn-primary&#34;&gt;提交&lt; / button&gt;                     &lt; button id =&#34; new_country_cancel_button&#34;类型=&#34;按钮&#34; class =&#34; btn btn-secondary&#34;数据驳回=&#34;模态&#34;&GT;关闭&LT; /按钮&GT;                 &LT; / DIV&GT;         &LT; / DIV&GT;     &LT; / DIV&GT; &LT; / DIV&GT; &lt; form class =&#34; single_upload_form&#34; action =&#34; {%url&#39;上传&#39; dataType方法%}&#34;方法=&#34;后&#34;&GT;     {%csrf_token%}     &lt; div class =&#34; container&#34;&gt;     &lt; div class =&#34; row&#34;&gt;         &lt; div class =&#34; col&#34;&gt;             &lt; div class =&#34; {{form.name.css_classes}}&#34;&gt;                 {{form.PK_idSequence.label_tag}} {{form.PK_idSequence.errors}}&lt; / br&gt;&lt; span class =&#34; form_field&#34;&gt; {{form.PK_idSequence}}&lt; / span&gt;             &LT; / DIV&GT;         &LT; / DIV&GT;         &lt; div class =&#34; col&#34;&gt;             &lt; div class =&#34; {{form.name.css_classes}}&#34;&gt;                 {{form.country.label_tag}} {{form.country.errors}}&lt; / br&gt;&lt; span class =&#34; form_field&#34;&gt; {{form.country}}&lt; / span&gt;             &LT; / DIV&GT;         &LT; / DIV&GT;     &lt; div class =&#34; row&#34;&gt;         &lt; div class =&#34; col&#34;&gt;             &lt;输入类型=&#34;提交&#34;值=&#34;提交&#34; /&GT;         &LT; / DIV&GT;     &LT; / DIV&GT; &LT; /形式&GT; 我的jQuery.js: jQuery(document).ready(function(){ / * ###############国家下拉列表选择############### * /     //在上传表单中,如果国家/地区下拉列表设置为新建...创建模式以获取新国家/地区     $(&#34;#id_country&#34)变化(函数(){。         var selectedValue = $(this).find(&#34; option:selected&#34;)。attr(&#34; value&#34;);         switch(selectedValue){             案例&#34;新&#34;:                 $(&#39;#modal_new_country&#39;)模式。(&#39;秀&#39;);                 打破;         }     });     //将新国家/地区添加到选择选项中     $(&#34; #new_country_button&#34;)。click(function(){         var newCountry = $(&#34; #new_country_text&#34;)。val();         newCountry = newCountry.substr(0,1).toUpperCase()+ newCountry.substr(1);         $(&#39;#id_country&#39;)。附加(&#39;&lt;选项值=&#34;&#39; + newCountry +&#39;&#34;选择=&#34;已选择& #34;&GT;&#39 + + newCountry&#39;&LT; /选项&GT;&#39);         $(&#39;#modal_new_country&#39;)模式。(&#39;隐藏&#39;);     });     //取消按钮用于模态国家/地区设置选择选项到&#39;?&#39;     $(&#34; #new_country_cancel_button&#34;)。click(function(){         $(&#39;#id_country&#39;)VAL。(&#39;&#39;);         $(&#39;#modal_new_country&#39;)模式。(&#39;隐藏&#39;);     }); }) 我想问题来自于select id_country中的新值不是来自表单的初始选项的一部分,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果它可以提供帮助,我找到了一种向<option>添加新<select> #id_country的方法。在我的forms.py中,我将ChoiceField替换为CharField哪个小部件是forms.Select,我放置了countryChoices

country = ChoiceField(choices = countryChoices, initial='?', required = False)

通过

country = CharField(max_length=50, widget=forms.Select(choices=countryChoices), required=False)

因此,由于国家/地区属性为CharFieldinvalid_choice不再提出验证错误,而form cleaned_data现在已为密钥'country'提供新值