- 浏览: 54925 次
- 性别:
- 来自: 西安
文章分类
最新评论
-
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 25821.os.system(cmd) 只能得到成功或者失败信息。 ... -
Python 有用库收集--持续更新中
2010-08-24 08:58 12291.控制台下的进度库 http://pypi.pyth ... -
好记性不如烂笔头--Django学习记录!
2010-07-19 12:51 0因为同时做两个项目,一直没有静下心来完整阅读django的文档 ... -
python 常用类库!
2010-07-01 11:55 6900Python学习 On this pag ... -
Django 资源收藏
2010-06-23 11:08 841非常全的资料 http://code.djangoproje ... -
Django meta
2010-03-24 10:23 1022通过一个内嵌类 "class Meta" ... -
Python 常用模块
2009-12-23 09:12 1302Python 常用模块 来自:http://www. ... -
Django on Nginx
2009-12-18 15:29 1243http://wiki.freebsdchina.org/ ... -
python re 模块
2009-12-17 16:13 1238正则表达式与 re 模块 一个正则表达式就是一个用来表示 ... -
memcached全面剖析
2009-12-10 16:14 1864idv2.com memcached全面剖析 长野雅 ... -
mysq 名录备忘
2009-12-08 20:58 8561:使用SHOW语句找出在服 ... -
django 备忘录 (转)记录django中小窍门
2009-11-27 09:00 946django forms中widget属性的自定义 ... -
DJANGO 数据库API (转)
2009-11-25 09:27 1725数据库 API 参考 作者: Django ... -
python shutil学习
2009-06-25 09:07 0shutil模块提供了一些高层次的文件操作, 比如复制, 设置 ...
相关推荐
**PyPI 官网下载 | django_widgets-0.1.15.tar.gz** 在Python的世界里,`PyPI`(Python Package Index)是官方的第三方软件包仓库,它为Python开发者提供了一个集中化的地方来发布、查找和安装软件包。这个资源`...
本文将深入探讨Django表单的Widgets的使用,以及如何自定义它们的样式和参数。 首先,理解Widget与字段(fields)的区别至关重要。字段负责数据的验证和模板中的呈现,而Widget则关注HTML表单元素的渲染。每个字段...
django-checkboxselectmultiple 使用多个复选框代替<select multiple> Django 小部件。 使用它而不是 Django 的默认值的好处是更好地与 Django Admin 集成。 Javascript 基于 Stefano Contini 的代码 安装 ...
**PyPI 官网下载 | django_map_widgets-0.3.0-py3-none-any.whl** `django_map_widgets-0.3.0-py3-none-any.whl` 是一个在Python生态系统中常见的轮子(wheel)文件,它是Python软件包的二进制格式,用于简化安装...
资源分类: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
widgets = { 'image': forms.ClearableFileInput(), } ``` 4. **Layui前端页面**: 在HTML模板文件(如`upload_image.html`)中,使用Layui的组件创建上传表单。Layui的`upload`模块可以帮助我们实现图片预览...
资源分类:Python库 所属语言:Python 资源全名:django-social-widgets-0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
为了在表单中显示FCKeditor,你需要创建一个自定义表单或者使用Django的ModelForm,并在表单的Meta类中指定`widgets`,将`FCKEditorWidget`用于`content`字段: ```python from django import forms from .models ...
Django是一个高级Python Web框架,它鼓励快速开发和干净、实用的设计。它具有许多功能,可以处理数据密集型的网站,无需花费太多的时间。在Web开发中,表单是数据交互的基本组成部分,Django为此提供了丰富的支持。 ...
- **第6章:Widgets**:讲解TurboGears提供的Widgets组件库,帮助开发者轻松创建丰富的用户界面。 - **第7章:高级TurboGears主题**:介绍一些高级特性,如自定义模板引擎、国际化支持等。 5. **Django部分**: ...
**django_material_widgets:使用Web的Material Components样式化的Django小部件** `django_material_widgets` 是一个优秀的Django应用,它允许开发者将Google的Material Design组件引入到Django表单和模型小部件中...
widgets = { 'content': forms.Textarea(attrs={'class': 'tinymce'}), } ``` 在这里,我们使用了`forms.Textarea`并添加了一个`tinymce`类,这将在前端触发TinyMCE的初始化。 现在,我们需要在视图`views.py`...
基础: 概览 | 表单 API | 内建字段 | 内建 widgets 进阶: 针对模型的表单 | 整合媒体 | 表单集 | 自定义验证 开发进程 学习众多的组件及工具,来帮助你开发和测试 Django 应用: 设置: 概览 | 完整的设置列表 应用...
资源分类:Python库 所属语言:Python 资源全名:django_widgets-0.1.11.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
第4章至第7章深入探讨了TurboGears的核心特性和高级话题,如用户身份验证、访问者追踪、小部件(widgets)的使用以及框架的高级特性,使读者能够熟练掌握TurboGears的使用方法。 ### 高级客户端主题 除了服务器端...