2017-05-25 56 views
4

我剛從Django 1.10更新到1.11.1。 在我的模板new_house_edit.html我有以下幾點:Django 1.11水平選擇字段

{{ form.rating }}

models.py包含以下內容:

class NewHouse(models.Model): 
    rating = models.IntegerField(choices=(
            (1, "1"), 
            (2, "2"), 
            (3, "3"), 
            (4, "4"), 
            (5, "5"), 
            ), 
            default=3 
          ) 

forms.py我曾經有以下幾點:

class HorizontalRadioRenderer(forms.RadioSelect.renderer): 
    def render(self): 
     return mark_safe(u'\n'.join([u'%s\n' % w for w in self])) 

class NewHouseForm(forms.ModelForm): 

    class Meta: 
     model = NewHouse 
     fields = (
       'rating',) 
     widgets={ 
       "rating": forms.RadioSelect(renderer=HorizontalRadioRenderer), 
       } 

這給了以下錯誤AttributeError: type object 'RadioSelect' has no attribute 'renderer'.我試圖通過doin解決它g這是不工作的:

class HorizontalRadioSelect(forms.RadioSelect): 
    template_name = 'new_house_edit' 


class NewHouseForm(forms.ModelForm): 

    class Meta: 
     model = NewHouse 
     fields = (
       'rating',) 
     widgets={ 
       "rating": "rating": forms.ChoiceField(widget=HorizontalRadioSelect, choices=(1, 2, 3, 4, 5)), 
       } 

我現在得到錯誤AttributeError: 'ChoiceField' object has no attribute 'use_required_attribute'。誰能幫我解決這個問題嗎?

+1

Tim Graham回答了一個相關問題[https://groups.google.com/forum/#!topic/django-users/tlcXfeSVm00],這可能會有所幫助。 – raratiru

回答

6

在您的第二個代碼片段中,您將表單字段對象傳遞給窗口小部件而不是窗口小部件對象。所以,正確的代碼如下所示:

class HorizontalRadioSelect(forms.RadioSelect): 
    template_name = 'horizontal_select.html' 


class NewHouseForm(forms.ModelForm): 

    class Meta: 
     model = NewHouse 
     fields = (
       'rating',) 
     widgets={ 
       "rating": HorizontalRadioSelect() 
       } 

在你的應用程序目錄中創建模板文件夾,並添加horizo​​ntal_select.html與下面的HTML。

{% with id=widget.attrs.id %} 
    <ul{% if id %} id="{{ id }}"{% endif %}{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}> 
     {% for group, options, index in widget.optgroups %} 
      {% if group %} 
       <li>{{ group }} 
       <ul{% if id %} id="{{ id }}_{{ index }}"{% endif %}> 
      {% endif %} 
      {% for option in options %} 
       <li style="display: inline-block">{% include option.template_name with widget=option %}</li> 
      {% endfor %} 
      {% if group %} 
       </ul> 
       </li> 
      {% endif %} 
     {% endfor %} 
    </ul> 
{% endwith %} 
+0

謝謝。它刪除了屬性錯誤,但給了我一個與模板名相關的新錯誤。我給了我使用評分的模板名稱。我認爲這是不正確的。 template_name應該指什麼? – Wessi

+0

很難,但最後我可以做到這一點:使用兩個模板選項。 – SalahAdDin

1

首先,ChoiceField不是一個小工具 - 這是一個Form Field

因此,改變你的形式,

class NewHouseForm(forms.ModelForm): 
    CHOICES = ((1, "1"), 
       (2, "2"), 
       (3, "3"), 
       (4, "4"), 
       (5, "5")) 

    rating = forms.ChoiceField(choices=CHOICES, 
     widget=forms.RadioSelect(attrs={'class': 'radio-inline'}), 
     ) 

    class Meta: 
     model = NewHouse 
     fields = (
      'rating',) 

然後自定義無線電選template_name應該到的路徑是您正在使用的模板。
如果模板位於app_level「templates」子目錄或您的項目級「templates」子目錄下,則可以像下面那樣執行。

class HorizontalRadioSelect(forms.RadioSelect): 
    template_name = 'new_house_edit.html' 

如果它在「模板」子目錄下的另一個子目錄下,則需要指定模板的路徑。

Django默認檢查項目中每個應用程序的「templates」子目錄中的模板。

+0

我沒有收到任何錯誤,但它顯示下拉菜單而不是水平無線電選擇。該模板位於「templates」子目錄中名爲「app」的子文件夾中。所以我做到了這一點:template_name ='app \ templates \ app \ new_house_edit.html' – Wessi

+1

路徑「app/new_house_edit.html」就足夠了...... – zaidfazil

+0

好的,但我仍然有同樣的問題。它顯示的下拉菜單中包含選項,而不是水平無線電選擇。 – Wessi

1

我知道這是一個相當古老的問題,但今天花了一些時間在各種線程(從1.8升級到1.11後)嘗試不同的方法,這裏是我的解決方案。

對於每個無線電設備組中,定義在模板中的以下(其中,「radio_list」是字段的名稱):

{% for radio in radio_list %} 
    {{ radio }} 
{% endfor %} 

這是它在它的最簡單的。容易嗎?你基本上可以應用任何你想要的樣式。全部都在RadioSelect部分的documentation中。

這個小故事的寓意是什麼?閱讀文檔。 Django可能是我曾經使用過的最好記錄的應用程序。如果我早些時候做了這件事,那麼今天我可以做更多有趣的事情會爲我節省很多時間。

希望這可以幫助別人。

+0

@isedwards - 感謝您的編輯。我錯過了。 – ElPedro