Login

HTML Widgets

Author:
robhudson
Posted:
February 26, 2008
Language:
Python
Version:
.96
Score:
-2 (after 2 ratings)

A simple widget library to provide HTML4 widgets for HTML validation fanatics.

 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.