开发者

Field.initial in views.py Django?

I read http://docs.djangoproject.com/en/dev/ref/forms/fields/

>>> class CommentForm(forms.Form):
...     name = for开发者_StackOverflow中文版ms.CharField(initial='Your name')
...     url = forms.URLField(initial='http://')
...     comment = forms.CharField()
>>> f = CommentForm(auto_id=False)
>>> print f
<tr><th>Name:</th><td><input type="text" name="name" value="Your name" /></td></tr>
<tr><th>Url:</th><td><input type="text" name="url" value="http://" /></td></tr>
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>

In my case I want initial field in views.py because My initial data(myinit_data) I get it in views.py

my forms.py

   #...
    class SendMailForm(forms.Form):
      to = forms.EmailField(initial=myinit_data, widget = forms.TextInput(attrs={'size': '50'}))
      subject = forms.CharField(widget = forms.TextInput(attrs={'size': '50'}))
      body = forms.CharField(widget = forms.Textarea(attrs={'cols': '50', 'rows': '10'}), required=False)

my views.py

  #....
    myinit_data = list_of_data_generated # for example [a@mail.com,b@mail.com,c@mail.com]
    form_sendmail = SendMailForm()

How Could I Initial my field from views.py

thanks! :)


As the documentation states, you can pass it initial data to the form constructor:

form_sendmail = SendMailForm(initial=myinit_data)

However myinit_data will have to be a dictionary, with the keys as the field names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜