2010-08-10 66 views
1

請原諒這個長代碼,但問題本身很短。Django不生成代表布爾字段的html選擇列表中的「selected ='selected'」

使用Django 1.2.1與MySQL服務器版本:Python的2.5

5.1.37與MySQLdb的1.2.2模型

QUALIFICATION_TYPE_CHOICES = ((1, 'WGH'), (2, 'PQR')) 
class Qualification(models.Model): 
    name = models.CharField(max_length=50) 
    qualification_type = models.PositiveSmallIntegerField(choices=QUALIFICATION_TYPE_CHOICES) 
    is_active = models.BooleanField("Item status",db_index=True,help_text="Only active items are visible on rest of this website") 
    def __unicode__(self): 
     return u'%s' % (self.name) 
    class Meta: 
     ordering = ['qualification_type', 'name'] 
     unique_together = (("qualification_type", "name"),) 

用自定義的ModelForm

STATUS_CHOICES = ((0, 'Inactive'), (1, 'Active')) 
class EditQualificationForm(forms.ModelForm): 
    name = forms.CharField(label='* Unique Name', max_length=50,help_text="(Required, max 50 characters)",widget=forms.TextInput(attrs={'class':'textInput',})) 
    qualification_type = forms.TypedChoiceField(label='Type',coerce=int,empty_value=None,choices=QUALIFICATION_TYPE_CHOICES,widget=forms.Select(attrs={'class':'selectInput',})) 
    is_active = forms.TypedChoiceField(label='* Is this Qualification active?',help_text="xyz",coerce=int,empty_value=0, choices=STATUS_CHOICES,widget=forms.Select(attrs={'class':'selectInput',})) 
    class Meta: 
     model = Qualification 

模板代碼

{% if form.non_field_errors %} 
    <div class="error"> 
    {% for error in form.non_field_errors %} 
     <p class="errorField"><strong>{{ error }}</strong></p> 
    {% endfor %} 
    </div> 
{% endif %} 

{% for field in form.visible_fields %} 
    {% if field.errors %} 
     <div class="ctrlHolder error"> 
     {% for error in field.errors %} 
      <p class="errorField"><strong>{{ error }}</strong></p> 
     {% endfor %} 
    {% else %} 
     <div class="ctrlHolder"> 
    {% endif %} 
     {{ field.label_tag }} 
     {{ field }} 
     <p class="formHint">{{ field.help_text }}</p> 
     </div> 
{% endfor %} 

{% for hidden in form.hidden_fields %} 
    {{ hidden }} 
{% endfor %} 

正在產生以下輸出

<div class="ctrlHolder"> 
    <label for="id_qualification_type">Type</label> 
    <select id="id_qualification_type" class="selectInput" name="qualification_type"> 
     <option value="1" selected="selected">WGH</option> 
     <option value="2">PQR</option> 
    </select> 
    <p class="formHint"></p> 
</div> 

<div class="ctrlHolder"> 
    <label for="id_is_active">* Is this Qualification active?</label> 
    <select id="id_is_active" class="selectInput" name="is_active"> 
     <option value="0">Inactive</option> 
     <option value="1">Active</option> 
    </select> 
    <p class="formHint">xyz</p> 
</div> 

所以對於qualification_type的HTML選擇列表是得到一個合適<option value="1" selected="selected">WGH</option>但IS_ACTIVE適當選項是沒有得到選擇(沒有在IS_ACTIVE生成的HTML沒有selected="selected")。

這用於早期工作。我已經將布爾選項映射爲0和1,並且很好地適合於mysql和python。我不知何故錯過了在什麼時候停止生成正確的html。但我很積極,它在Django-1.2.1之前就起作用了。

+0

更改STATUS_CHOICES =((0, '無效'),(1, '有效'))到STATUS_CHOICES =((假, '無效'),(真, '有效')),並且迫使= int to coerce = bool可能會解決這個問題,但我想知道爲什麼它現在停止工作,而它早先完美。我無法在Django文檔或發行說明中找到任何內容,這就是我要問的原因。 – chefsmart 2010-08-11 02:28:07

+0

爲了記錄,我最初開始使用STATUS_CHOICES =((False,'Inactive'),(True,'Active')) - 這是在Django1.0之前的日子裏。它沒有工作,所以我去使用STATUS_CHOICES =((0,'Inactive'),(1,'Active'))。現在看起來一切都已經完成(見下面的答案中的鏈接)。 – chefsmart 2010-08-11 08:27:21

回答

0

我猜測它與is_active表單字段中不必要的empty_value有關。

+0

我通過刪除empty_value來檢查,但得到了相同的結果。就像我剛纔提到的那樣,這在早些時候工作得很好 – chefsmart 2010-08-11 02:20:52

相關問題