django form的widgets类型列表 -凯发k8网页登录

--- 博客已迁移至:

  凯发k8网页登录-凯发天生赢家一触即发官网 :: 凯发k8网页登录首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  618 随笔 :: 87 文章 :: 225 评论 :: 0 trackbacks

a widget is django’s representation of a html input element. the widget handles the rendering of the html, and the extraction of data from a get/post dictionary that corresponds to the widget.

django provides a representation of all the basic html widgets, plus some commonly used groups of widgets:

class textinput
text input:
class passwordinput

password input:

takes one optional argument:

render_value
determines whether the widget will have a value filled in when the form is re-displayed after a validation error (default is true).
class hiddeninput
hidden input:
class multiplehiddeninput
multiple widgets.
class fileinput
file upload input:
class dateinput
new in django 1.1:

date input as a simple text box:

takes one optional argument:

format
the format in which this field’s initial value will be displayed.

if no format argument is provided, the default format is '%y-%m-%d'.

class datetimeinput
new in django 1.0:

date/time input as a simple text box:

takes one optional argument:

format
the format in which this field’s initial value will be displayed.

if no format argument is provided, the default format is '%y-%m-%d %h:%m:%s'.

class timeinput

time input as a simple text box:

takes one optional argument:

format
the format in which this field’s initial value will be displayed.

if no format argument is provided, the default format is '%h:%m:%s'.

changed in django 1.1: the format argument was not supported in django 1.0.
class textarea
text area:
class checkboxinput

checkbox:

takes one optional argument:

check_test
a callable that takes the value of the checkboxinput and returns true if the checkbox should be checked for that value.
class select

select widget:

requires that your field provides choices.

class nullbooleanselect
select widget with options ‘unknown’, ‘yes’ and ‘no’
class selectmultiple

select widget allowing multiple selection:

requires that your field provides choices.

class radioselect

a list of radio buttons:



  • ...

requires that your field provides choices.

class checkboxselectmultiple

a list of checkboxes:



  • ...
class multiwidget
wrapper around multiple other widgets
class splitdatetimewidget

wrapper around two widgets: dateinput for the date, and timeinput for the time.

takes two optional arguments, date_format and time_format, which work just like the format argument for dateinput and timeinput.

changed in django 1.1: the date_format and time_format arguments were not supported in django 1.0.
class selectdatewidget

wrapper around three select widgets: one each for month, day, and year. note that this widget lives in a separate file from the standard widgets.

from django.forms.extras.widgets import selectdatewidget

date = forms.datefield(widget=selectdatewidget())

specifying widgets

form.widget

whenever you specify a field on a form, django will use a default widget that is appropriate to the type of data that is to be displayed. to find which widget is used on which field, see the documentation for the built-in field classes.

however, if you want to use a different widget for a field, you can - just use the 'widget' argument on the field definition. for example:

from django import forms

class commentform(forms.form):
name = forms.charfield()
url = forms.urlfield()
comment = forms.charfield(widget=forms.textarea)

this would specify a form with a comment that uses a larger textarea widget, rather than the default textinput widget.

customizing widget instances

when django renders a widget as html, it only renders the bare minimum html - django doesn't add a class definition, or any other widget-specific attributes. this means that all 'textinput' widgets will appear the same on your web page.

if you want to make one widget look different to another, you need to specify additional attributes for each widget. when you specify a widget, you can provide a list of attributes that will be added to the rendered html for the widget.

for example, take the following simple form:

class commentform(forms.form):
name = forms.charfield()
url = forms.urlfield()
comment = forms.charfield()

this form will include three default textinput widgets, with default rendering - no css class, no extra attributes. this means that the input boxes provided for each widget will be rendered exactly the same:

>>> f = commentform(auto_id=false)
>>> f.as_table()
name:
url:
comment:

on a real web page, you probably don't want every widget to look the same. you might want a larger input element for the comment, and you might want the 'name' widget to have some special css class. to do this, you use the attrs argument when creating the widget:

widget.attrs

for example:

class commentform(forms.form):
name = forms.charfield(
widget=forms.textinput(attrs={'class':'special'}))
url = forms.urlfield()
comment = forms.charfield(
widget=forms.textinput(attrs={'size':'40'}))

django will then include the extra attributes in the rendered output:

>>> f = commentform(auto_id=false)
>>> f.as_table()
name:
url:
comment:
posted on 2009-09-27 15:35 seal 阅读(3145) 评论(2)     所属分类: python
# re: django form的widgets类型列表 2014-06-27 17:49
你这么一贴文档,你自己还会再看第二遍么。。  回复  
  

# re: django form的widgets类型列表 2015-12-18 13:51
额请问请问其味无穷而且  回复  
  


只有注册用户后才能发表评论。


网站导航:
              
相关文章:
 
网站地图