1
我需要一個整數表單字段有一個'默認'值。但是我需要在運行時定義這個值。Django:動態IntegerField初始(呈現空)
我不喜歡它的部份:
# at my form constructor:
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, initial=None, error_class=ErrorList,
label_suffix=':', empty_permitted=False):
super(FlatSearchForm, self).__init__(data, files, auto_id, prefix, initial, error_class, label_suffix,
empty_permitted)
min_price = ... # some code to find the minimum price
self.price_min = min_price # will be used later
max_price = ... # some code to find the maximum price
self.price_max = max_price
# here we go:
self.fields['price_from'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=min_price)
self.fields['price_to'] = forms.IntegerField(min_value=min_price, max_value=max_price, initial=max_price)
# ...
# how I use the form (in views.py):
class SearchFlatView(ListView):
model = Flat
context_object_name = 'flats'
template_name = 'catalog/search.html'
def get_context_data(self, **kwargs):
context = super(SearchFlatView, self).get_context_data(**kwargs)
context['form'] = FlatSearchForm(self.request.GET)
return context
的問題是:值未在表格所示,當我渲染使用{{form.price_from}}和場{{form.price_to}} - 這些領域只是空的!
你能告訴我我做錯了什麼嗎?
你如何使用表格?你如何實例化它?它是綁定還是非綁定? – Tisho 2012-07-13 08:34:41
更新了代碼。一探究竟。 – 2012-07-13 08:40:49