Python---The form component in Django (2) customizing properties and various validation of forms

Source: Internet
Author: User

Analysis Widget:

class Testform (forms. Form):    = Fields . Charfield (            = True,            = widgets. TextInput ()    )

Tracking widgets.py

__all__ = (    'Media','Mediadefiningclass','Widgets','TextInput','Numberinput',    'EmailInput','Urlinput','Passwordinput','Hiddeninput',    'Multiplehiddeninput','FileInput','Clearablefileinput','Textarea',    'Dateinput','Datetimeinput','Timeinput','Checkboxinput','Select',    'Nullbooleanselect','Selectmultiple','Radioselect',    'Checkboxselectmultiple','Multiwidget','Splitdatetimewidget',    'Splithiddendatetimewidget','Selectdatewidget',)
All Controls
class TextInput (Input):     ' text ' ' django/forms/widgets/text.html'  #模板html存放路径      

To trace the parent class:

classInput (Widget):"""Baseclass  forAll <input>widgets. """Input_type = None # subclasses must define This. Template_name='django/forms/widgets/input.html'def __init__ (self, attrs=None): #发现这里又参数attrs可以设置属性ifAttrs isNot none:attrs=attrs.copy () Self.input_type= Attrs.pop ('type', Self.input_type) super (input, self). __init__ (ATTRS) def get_context (self, name, value, attrs): cont Ext=Super (Input, self). Get_context (name, value, attrs) context['Widgets']['type'] =Self.input_typereturnContext

Found template file Template_name = ' django/forms/widgets/input.html ', actually does not exist, is called the parent class method

classWidget (Six.with_metaclass (Renamewidgetmethods)): Def get_context (self, name, value, Attrs): Context={} context['Widgets'] = {            'name': Name,'Is_hidden': Self.is_hidden,'Required': self.is_required,'value': Self.format_value (value),'Attrs': Self.build_attrs (Self.attrs, attrs),'Template_name': Self.template_name,}returnContext def render (self, name, value, Attrs=none, renderer=None):"""        Returns this Widget rendered as HTML, as a Unicode string. #生成对于html代码, return to use"""Context =self.get_context (name, value, Attrs)returnSelf._render (self.template_name, Context, renderer) def _render (self, template_name, context, renderer=None):ifRenderer isNone:renderer=Get_default_renderer ()returnMark_safe (Renderer.render (template_name, context))

So we can customize the style, properties

widget = widgets. TextInput (attrs={"class": "C1"}), #这个属性, to create the desired style by setting it on the front end

Add: Generate native strings on the server and do not escape when front-end rendering is required

" <input type= ' text '/> " = Mark_safe (txt) #前端可以正常显示  

Select Radio Box:

sel_inp= fields. Choicefield (    = [(1,'a'), (2,'b' ) ),])

Select box:

SEL_INP = Fields . Charfield (        = widgets. Select (choices=[(1,'a'), (2,'b ' ),]))

Combo Multi-Select:

radio_inp= fields. Multiplechoicefield (    = [(1,'a'), (2,'b ' ),]   #含有multiple时可以写在外, can also be written in, here is recommended    outside = widgets. Selectmultiple (attrs={'class':"C1"})) 

Radio checkbox:

    CHK_INP = Fields . Charfield (        = widgets. Checkboxinput ()    )

Multi-Select checkbox

    MCHK_INP = Fields . Multiplechoicefield (        = widgets. Checkboxselectmultiple (),        choices=[(1"D"), (2" e "), (3,'r')],        = [2,3 ]    )

Radio Radio:

    RAD_INP = Fields . Choicefield (        choices=[(1," male "), (2," female " ),],        initial=2,        =widgets. Radioselect (),    )

Fields are used to hold regular expressions, and HTML plugins are used to produce HTML tags (input)

Python---The form component in Django (2) customizing properties and various validation of forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.