tags:

views:

53

answers:

1

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

>>> class CommentForm(forms.Form):
...     name = forms.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 [[email protected],[email protected],[email protected]]
    form_sendmail = SendMailForm()

How Could I Initial my field from views.py

thanks! :)

+3  A: 

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.

Daniel Roseman
thank for you help
python
"However myinit_data will have to be a dictionary, with the keys as the field names."I dont know exactly How to initial for specific field in form.forms.EmailField for example.
python
`{'myfieldname':'myvalue'}` - sounds like you need a basic introduction to Python, try some of the tutorials listed at http://wiki.python.org/moin/BeginnersGuide/NonProgrammers
Daniel Roseman