- 浏览: 53409 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
iamicebergs:
好东西,收藏~
[转]用python爬虫抓站的一些技巧总结 -
qiaoqinqie:
mark 很好的学习资料
python 常用类库! -
fire01312:
svn import /tmp/skysrt file:/// ...
FEDORA下SVN的安装以及配置 所属分类:Linux 评论(1),浏览(94)
Widgets¶
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:
Password input: <input type='password' ...>
Takes one optional argument:
Date input as a simple text box: <input type='text' ...>
Takes one optional argument:
If no format argument is provided, the default format is '%Y-%m-%d' .
Date/time input as a simple text box: <input type='text' ...>
Takes one optional argument:
If no format argument is provided, the default format is '%Y-%m-%d %H:%M:%S' .
Time input as a simple text box: <input type='text' ...>
Takes one optional argument:
If no format argument is provided, the default format is '%H:%M:%S' .
Checkbox: <input type='checkbox' ...>
Takes one optional argument:
Select widget: <select><option ...>...</select>
Requires that your field provides choices .
Select widget allowing multiple selection: <select multiple='multiple'>...</select>
Requires that your field provides choices .
A list of radio buttons:
<ul>
<li><input type='radio' ...></li>
...
</ul>
Requires that your field provides choices .
A list of checkboxes:
<ul>
<li><input type='checkbox' ...></li>
...
</ul>
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 .
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¶
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
()
<tr><th>Name:</th><td><input type="text" name="name" /></td></tr>
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
<tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr>
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:
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
()
<tr><th>Name:</th><td><input type="text" name="name" class="special"/></td></tr>
<tr><th>Url:</th><td><input type="text" name="url"/></td></tr>
<tr><th>Comment:</th><td><input type="text" name="comment" size="40"/></td></tr>
发表评论
-
python 获取shell命令返回值
2010-12-24 12:45 25411.os.system(cmd) 只能得到成功或者失败信息。 ... -
Python 有用库收集--持续更新中
2010-08-24 08:58 11901.控制台下的进度库 http://pypi.pyth ... -
好记性不如烂笔头--Django学习记录!
2010-07-19 12:51 0因为同时做两个项目,一直没有静下心来完整阅读django的文档 ... -
python 常用类库!
2010-07-01 11:55 6830Python学习 On this pag ... -
Django 资源收藏
2010-06-23 11:08 807非常全的资料 http://code.djangoproje ... -
Django meta
2010-03-24 10:23 989通过一个内嵌类 "class Meta" ... -
Python 常用模块
2009-12-23 09:12 1275Python 常用模块 来自:http://www. ... -
Django on Nginx
2009-12-18 15:29 1214http://wiki.freebsdchina.org/ ... -
python re 模块
2009-12-17 16:13 1178正则表达式与 re 模块 一个正则表达式就是一个用来表示 ... -
memcached全面剖析
2009-12-10 16:14 1806idv2.com memcached全面剖析 长野雅 ... -
mysq 名录备忘
2009-12-08 20:58 8111:使用SHOW语句找出在服 ... -
django 备忘录 (转)记录django中小窍门
2009-11-27 09:00 906django forms中widget属性的自定义 ... -
DJANGO 数据库API (转)
2009-11-25 09:27 1658数据库 API 参考 作者: Django ... -
python shutil学习
2009-06-25 09:07 0shutil模块提供了一些高层次的文件操作, 比如复制, 设置 ...
相关推荐
主要介绍了django表单的Widgets,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
资源来自pypi官网。 资源全名:django_widgets-0.1.15.tar.gz
资源分类:Python库 所属语言:Python 资源全名:django-ui-widgets-1.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
pip install django-map-widgets 在settings.py中将“ map_widgets”添加到您的INSTALLED_APPS INSTALLED_APPS = [ ... ' django . contrib . sessions ', 'django.contrib.messages' , 'django.contrib....
python库。资源全名:django-ui-widgets-1.2b0.tar.gz
资源分类:Python库 所属语言:Python 资源全名:django-social-widgets-0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
资源来自pypi官网。 资源全名:django_map_widgets-0.3.0-py3-none-any.whl
Leveraging the Power of Django 12 Leveraging the Power of Python 12 Comparing the Frameworks 13 The Historical Perspective 13 Controller Invocation 14 Summary 17 Chapter 2: Web 2.0 with ...
基础: 概览 | 表单 API | 内建字段 | 内建 widgets 进阶: 针对模型的表单 | 整合媒体 | 表单集 | 自定义验证 开发进程 学习众多的组件及工具,来帮助你开发和测试 Django 应用: 设置: 概览 | 完整的设置列表 应用...
资源分类:Python库 所属语言:Python 资源全名:django_widgets-0.1.11.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
django_material_widgets:使用Web的Material Components样式化的Django小部件
Django小部件,用于使用浏览器内代码编辑器CodeMirror替换文本区域。 在Django 1.6+上测试,支持Python 2.7和Python 3.4+。...用法from django import formsfrom codemirror2.widgets import CodeMir
e git+/path/to/github/django-period-widget#egg=django_period_widget 将django_period_widget添加到INSTALLED_APPS 运行python manage.py collectstatic用法创建表格: from django.contrib.admin.widgets import...
使用Django-Ueditor非常简单,方法如下: 1、安装方法 **方法一:下载安装包,在命令行运行: python setup.py install **方法二:使用pip工具在命令行运行(推荐): pip install DjangoUeditor 2、在INSTALL...
class HTML5DateTimeInput(django.forms.widgets.DateTimeInput): """Subclass TextInput since there's no direct way to override its 'type' attribute""" if settings.USE_TZ: input_type = 'datetime' ...
django-bootstrap3-datetimepicker 该软件包使用以下项目提供的... from bootstrap3_datetime.widgets import DateTimePicker from django import forms class ToDoForm(forms.Form): todo = forms.CharField
Django Pagedown 将到Django Admin或自定义表单中。要求django-pagedown > = 2.0.0版本需要Django 2.1.0或更高版本(以前的版本应一直支持Django直至1.1左右)。... widgets import AdminPagedownWidgetclass Alb
from django.forms import widgets as wdt # 因为重名,所以起个别名 #定义一个类,比如BookForm,这个类要继承ModelForm,在这个类中再写一个原类Meta(规定写法,注意首字母是大写的) #在这个原类中,有以下属性...