HTML5 Placeholder from FormField label

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django import template
from django.forms import Field

register = template.Library()

class PlaceholderForm(template.Node):
    def __init__(self, form_field):
        self.form_field =  form_field
    
    def render(self, context):
        html = self.form_field.resolve(context)
        label = html.label
        return html.as_widget(attrs={'placeholder':label})


@register.tag
def form_placeholder(parser, token):
    try:
        tag_name, form_field_string = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError('{0} tag requires a single argument'.format( token.contents.split()[0]))
    form_field = parser.compile_filter(form_field_string)
    return PlaceholderForm(form_field)

More like this

  1. StaticField for non-changing text data in forms by V 3 years, 11 months ago
  2. Select Dropdown Widget with jQueryUI by maguspk 2 years, 11 months ago
  3. Custom Widget Types for HTML5 Form Fields by leveillej 3 years ago
  4. UsernameField (for clean error messages) by davepeck 3 years, 9 months ago
  5. ISBN model field: displays 10- and 13-digit variants and external links by fish2000 3 years, 1 month ago

Comments

(Forgotten your password?)