我在django1.3应用程序中使用django表单。
以下是forms.py
from django import forms
class AccountForm(forms.ModelForm):
class Meta:
model = Account
fields = ('name', 'address',
'city', 'state',
'country', 'telephone',)
在views.py中,我正在使用django profiles并像这样传递AccountForm
,
from profiles.views import profile_detail, edit_profile
def profile_update(request):
# some code here
# some more code here
# to set extra_context
return edit_profile(request, extra_context=extra_context,
form_class=AccountForm,
success_url='/some/success/url/here')
html文件呈现如下,
{% for field in form %}
<label>{{field.label|safe }}</label>
{{ field }}
{% endfor %}
我正在寻找一种在某种条件下选择性地包含表单中字段的方法,例如基于Account
模型中字段的值。
示例如果Account.joining_date > datetime.date(1,3,2010)
只在表单中包含字段telephone
,否则不包括。我如何实现此功能?如果需要获得解决方案,我很乐意提供任何额外信息:)
提前致谢!