| | 2707 | # Callable initial data ######################################################## |
| | 2708 | |
| | 2709 | The previous technique dealt with raw values as initial data, but it's also |
| | 2710 | possible to specify callable data. |
| | 2711 | |
| | 2712 | >>> class UserRegistration(Form): |
| | 2713 | ... username = CharField(max_length=10) |
| | 2714 | ... password = CharField(widget=PasswordInput) |
| | 2715 | |
| | 2716 | We need to define functions that get called later. |
| | 2717 | >>> def initial_django(): |
| | 2718 | ... return 'django' |
| | 2719 | >>> def initial_stephane(): |
| | 2720 | ... return 'stephane' |
| | 2721 | |
| | 2722 | Here, we're not submitting any data, so the initial value will be displayed. |
| | 2723 | >>> p = UserRegistration(initial={'username': initial_django}, auto_id=False) |
| | 2724 | >>> print p.as_ul() |
| | 2725 | <li>Username: <input type="text" name="username" value="django" maxlength="10" /></li> |
| | 2726 | <li>Password: <input type="password" name="password" /></li> |
| | 2727 | >>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False) |
| | 2728 | >>> print p.as_ul() |
| | 2729 | <li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> |
| | 2730 | <li>Password: <input type="password" name="password" /></li> |
| | 2731 | |
| | 2732 | The 'initial' parameter is meaningless if you pass data. |
| | 2733 | >>> p = UserRegistration({}, initial={'username': initial_django}, auto_id=False) |
| | 2734 | >>> print p.as_ul() |
| | 2735 | <li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> |
| | 2736 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
| | 2737 | >>> p = UserRegistration({'username': u''}, initial={'username': initial_django}, auto_id=False) |
| | 2738 | >>> print p.as_ul() |
| | 2739 | <li><ul class="errorlist"><li>This field is required.</li></ul>Username: <input type="text" name="username" maxlength="10" /></li> |
| | 2740 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
| | 2741 | >>> p = UserRegistration({'username': u'foo'}, initial={'username': initial_django}, auto_id=False) |
| | 2742 | >>> print p.as_ul() |
| | 2743 | <li>Username: <input type="text" name="username" value="foo" maxlength="10" /></li> |
| | 2744 | <li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /></li> |
| | 2745 | |
| | 2746 | A callable 'initial' value is *not* used as a fallback if data is not provided. |
| | 2747 | In this example, we don't provide a value for 'username', and the form raises a |
| | 2748 | validation error rather than using the initial value for 'username'. |
| | 2749 | >>> p = UserRegistration({'password': 'secret'}, initial={'username': initial_django}) |
| | 2750 | >>> p.errors |
| | 2751 | {'username': [u'This field is required.']} |
| | 2752 | >>> p.is_valid() |
| | 2753 | False |
| | 2754 | |
| | 2755 | If a Form defines 'initial' *and* 'initial' is passed as a parameter to Form(), |
| | 2756 | then the latter will get precedence. |
| | 2757 | >>> class UserRegistration(Form): |
| | 2758 | ... username = CharField(max_length=10, initial=initial_django) |
| | 2759 | ... password = CharField(widget=PasswordInput) |
| | 2760 | >>> p = UserRegistration(initial={'username': initial_stephane}, auto_id=False) |
| | 2761 | >>> print p.as_ul() |
| | 2762 | <li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> |
| | 2763 | <li>Password: <input type="password" name="password" /></li> |
| | 2764 | |