我使用Django django.forms.Form和django.views.generic.edit.FormView來呈現HTML模板。Django:表單失敗的動態初始值
我會爲一些在我的表單字段添加默認值,但它遇到了一個問題:
這裏是我的代碼:
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormView
class SignForm(forms.Form):
greeting_message = forms.CharField(label= 'Greeting message', widget=forms.Textarea, required=True, max_length=100)
book_name = forms.CharField(label='Guestbook name', max_length=10, required=True)
class SignView(FormView):
template_name = 'guestbook/sign.html'
form_class = SignForm(initial={'book_name':'aaaa'})#assign default value for field 'book_name'
def form_valid(self, form):
...
誰能幫助我?
'form_class = SignForm(...)'正在初始化表單,它應該簡單地定義要使用的表單。嘗試將其更改爲'form_class = SignForm',至於設置初始值,請看這個問題:http://stackoverflow.com/questions/22083218/django-how-to-pre-populate-formview-with-dynamic - 壬模型數據 – Wolph 2015-02-05 18:27:34