Exemple #1
0
    def test_text_input(self):
        """<input type="text">"""
        class TextForm(forms.Form):
            text = forms.CharField(label='My text field')

        rendered = TextForm().as_p()

        self.assertHTMLEqual(rendered, """
        <p>
            <label for="id_text">My text field:</label>
            <input class=" form-control" type="text" name="text" id="id_text" />
        </p>""")

        form = TextForm(data={'text': ''})
        self.assertFalse(form.is_valid())

        form = TextForm(data={'text': 'some text'})
        self.assertTrue(form.is_valid())

        class TextFormNotRequire(forms.Form):
            text = forms.CharField(required=False)

        rendered = TextFormNotRequire().as_p()
        self.assertHTMLEqual(rendered, """
        <p>
            <label for="id_text">Text:</label>
            <input class=" form-control" type="text" name="text" id="id_text">
        </p>""")

        class TextFormAttr(forms.Form):
            text = forms.CharField(
                widget=forms.TextInput(attrs={'placeholder': 'Heheheh'})
            )

        rendered = TextFormAttr(initial={'text': 'some initial text'}).as_p()
        self.assertHTMLEqual(rendered, """
        <p>
            <label for="id_text">Text:</label>
            <input class=" form-control" type="text" name="text" id="id_text" value="some initial text" placeholder="Heheheh" >
        </p>""")

        invalid = lambda: forms.CharField(max_length=5).clean('foo bar')
        self.assertRaises(forms.ValidationError, invalid)

        class TextFormMax(forms.Form):
            text = forms.CharField(max_length=2)
 
        self.assertFalse(TextFormMax(data={'text': 'foo'}).is_valid())
 
        rendered = TextFormMax(data={'text': 0}).as_p()
 
        self.assertHTMLEqual(rendered, """
        <p>
            <label for="id_text">Text:</label>
            <input class=" form-control" type="text" name="text" id="id_text" value="0" maxlength="2">
        </p>""")
Exemple #2
0
 class PwFormWidget(forms.Form):
     text = forms.CharField()
     pw = forms.CharField(widget=forms.PasswordInput)
Exemple #3
0
 class ComboForm(forms.Form):
     combo = forms.ComboField(fields=[forms.EmailField(),
                                      forms.CharField(max_length=10)])
Exemple #4
0
 class PwForm(forms.Form):
     pw = forms.CharField(widget=forms.PasswordInput)
Exemple #5
0
 class TextFormMax(forms.Form):
     text = forms.CharField(max_length=2)
Exemple #6
0
 class TextFormAttr(forms.Form):
     text = forms.CharField(
         widget=forms.TextInput(attrs={'placeholder': 'Heheheh'})
     )
Exemple #7
0
 class TextFormNotRequire(forms.Form):
     text = forms.CharField(required=False)
Exemple #8
0
 class TextForm(forms.Form):
     text = forms.CharField(label='My text field')
Exemple #9
0
 class SearchForm(forms.Form):
     query = forms.CharField(widget=forms.SearchInput)
Exemple #10
0
 class TextForm(forms.Form):
     text = forms.CharField(widget=forms.Textarea)
Exemple #11
0
 class TextFormAttrs(forms.Form):
     text = forms.CharField(
         widget=forms.Textarea(attrs={'rows': 42, 'cols': 55})
     )
Exemple #12
0
 class HiddenForm(forms.Form):
     hide = forms.CharField(widget=forms.HiddenInput())
Exemple #13
0
 class PwFormData(forms.Form):
     text = forms.CharField()
     pw = forms.CharField(
         widget=forms.PasswordInput(render_value=True)
     )
Exemple #14
0
 class Form(forms.Form):
     text = forms.CharField(widget=forms.TextInput(attrs={
         'foo': True,
         'bar': False,
     }))
Exemple #15
0
 class SelectFormWidget(forms.Form):
     foo = forms.CharField(widget=forms.Select(choices=choices),
                           required=False)
Exemple #16
0
 class SelectForm(forms.Form):
     foo = forms.CharField(widget=forms.Select(choices=choices))