| | 2755 | # Callable initial data ######################################################## |
|---|
| | 2756 | |
|---|
| | 2757 | The previous technique dealt with raw values as initial data, but it's also |
|---|
| | 2758 | possible to specify callable data. |
|---|
| | 2759 | |
|---|
| | 2760 | >>> class UserRegistration(Form): |
|---|
| | 2761 | ... username = CharField(max_length=10) |
|---|
| | 2762 | ... password = CharField(widget=PasswordInput) |
|---|
| | 2763 | |
|---|
| | 2764 | We need to define functions that get called later. |
|---|
| | 2765 | >>> def initial_django(): |
|---|
| | 2766 | ... return 'django' |
|---|
| | 2767 | >>> def initial_stephane(): |
|---|
| | 2768 | ... return 'stephane' |
|---|
| | 2769 | |
|---|
| | 2770 | Here, we're not submitting any data, so the initial value will be displayed. |
|---|
| | 2771 | >>> p = UserRegistration(initial={'username': initial_django}, auto_id=False) |
|---|
| | 2772 | >>> print p.as_ul() |
|---|
| | 2773 | <li>Username: <input type="text" name="username" value="django" maxlength="10" /></li> |
|---|
| | 2774 | <li>Password: <input type="password" name="password" /></li> |
|---|
| | 2775 | |
|---|
| | 2776 | The 'initial' parameter is meaningless if you pass data. |
|---|
| | 2777 | >>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False) |
|---|
| | 2778 | >>> print p.as_ul() |
|---|
| | 2779 | <li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> |
|---|
| | 2780 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
|---|
| | 2781 | >>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False) |
|---|
| | 2782 | >>> print p.as_ul() |
|---|
| | 2783 | <li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> |
|---|
| | 2784 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
|---|
| | 2785 | >>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False) |
|---|
| | 2786 | >>> print p.as_ul() |
|---|
| | 2787 | <li>Username: <input type="text" name="username" value="foo" maxlength="10" /></li> |
|---|
| | 2788 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
|---|
| | 2789 | |
|---|
| | 2790 | A callable 'initial' value is *not* used as a fallback if data is not provided. |
|---|
| | 2791 | In this example, we don't provide a value for 'username', and the form raises a |
|---|
| | 2792 | validation error rather than using the initial value for 'username'. |
|---|
| | 2793 | >>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django}) |
|---|
| | 2794 | >>> p.errors |
|---|
| | 2795 | {'username': [u'This field is required.']} |
|---|
| | 2796 | >>> p.is_valid() |
|---|
| | 2797 | False |
|---|
| | 2798 | |
|---|
| | 2799 | If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(), |
|---|
| | 2800 | then the latter will get precedence. |
|---|
| | 2801 | >>> class UserRegistration(Form): |
|---|
| | 2802 | ... username = CharField(max_length=10, initial=initial_django) |
|---|
| | 2803 | ... password = CharField(widget=PasswordInput) |
|---|
| | 2804 | >>> p = UserRegistration(auto_id=False) |
|---|
| | 2805 | >>> print p.as_ul() |
|---|
| | 2806 | <li>Username: <input type="text" name="username" value="django" maxlength="10" /></li> |
|---|
| | 2807 | <li>Password: <input type="password" name="password" /></li> |
|---|
| | 2808 | >>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False) |
|---|
| | 2809 | >>> print p.as_ul() |
|---|
| | 2810 | <li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> |
|---|
| | 2811 | <li>Password: <input type="password" name="password" /></li> |
|---|
| | 2812 | |
|---|