HTML Widgets

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
File: widgets/__init__.py
HTML4 Widget overrides
"""
from django.newforms.widgets import Input
from django.utils.safestring import mark_safe
from django.newforms.util import flatatt
from django.utils.encoding import StrAndUnicode, force_unicode

class HTML4Input(Input):
    input_type = None # Subclasses must define this.

    def render(self, name, value, attrs=None):
        if value is None: value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(value)
        return mark_safe(u'<input%s>' % flatatt(final_attrs))

class HTML4TextInput(HTML4Input):
    input_type = 'text'

class HTML4PasswordInput(HTML4Input):
    input_type = 'password'

class HTML4HiddenInput(HTML4Input):
    input_type = 'hidden'
    is_hidden = True

class HTML4FileInput(HTML4Input):
    input_type = 'file'
    needs_multipart_form = True

class HTML4DateTimeInput(HTML4Input):
    input_type = 'text'
    format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'


"""
File: myapp/forms.py
Forms based on HTML4 widgets
"""
from django import newforms as forms
from myproject import widgets

class ThreadForm(forms.Form):
    subject = forms.CharField(
        max_length=150, 
        widget=widgets.HTML4TextInput(attrs={'size': 60}),
        help_text="Something descriptive, maximum of 150 characters.")
    message = forms.CharField(
        widget=forms.Textarea(attrs={'cols': 60, 'rows': 10}), 
        help_text="Use of Markdown syntax is available, no HTML tags allowed.")

More like this

  1. NoneWidget by diverman 3 years, 9 months ago
  2. Admin related widget wrapper with edit / delete link (html) by nasp 2 years, 6 months ago
  3. Add model form field meta data in a DRY way by jedie 5 years, 1 month ago
  4. Sanitize text field HTML (here from the Dojo Toolkit Editor2 widget) by akaihola 7 years ago
  5. TinyMCE Widget by semente 4 years, 7 months ago

Comments

(Forgotten your password?)