0
請從數據庫中動態填寫下拉列表中的值。如何填充下拉列表表單?
models.py:
class Gender(models.Model):
gender = models.CharField(
max_length=10,
blank=False,
)
@classmethod
def get_gender_list(self):
return self.objects.all()
forms.py:
class SearchFriendsForm(forms.Form):
gender = forms.ChoiceField(
widget=forms.Select,
choices=Gender.get_gender_list(), # not worked
#choices=(('1', '1'), ('2', '2')), # worked
label='Пол',
)
friends_search.html:顯示
{{ form.gender }}
產生的誤差:
'Gender' object is not iterable
Request Method: GET
Request URL: http://127.0.0.1:8000/userprofile/friends_search/
Django Version: 1.6.2
Exception Type: TypeError
Exception Value:
'Gender' object is not iterable
Exception Location: C:\Python33\lib\site-packages\django\forms\widgets.py in render_options, line 528
這是解決問題。 thnanks! – user3607370