`

django Widgets

阅读更多

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:

<!-- [django.forms.TextInput]-->class TextInput
Text input: <input type='text' ...>
<!-- [django.forms.PasswordInput]-->class PasswordInput

Password input: <input type='password' ...>

Takes one optional argument:

<!-- [django.forms.PasswordInput.render_value]-->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 ).
<!-- [django.forms.HiddenInput]-->class HiddenInput
Hidden input: <input type='hidden' ...>
<!-- [django.forms.MultipleHiddenInput]-->class MultipleHiddenInput
Multiple <input type='hidden' ...> widgets.
<!-- [django.forms.FileInput]-->class FileInput
File upload input: <input type='file' ...>
<!-- [django.forms.DateInput]-->class DateInput
New in Django 1.1: Please, see the release notes

Date input as a simple text box: <input type='text' ...>

Takes one optional argument:

<!-- [django.forms.DateInput.format]-->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' .

<!-- [django.forms.DateTimeInput]-->class DateTimeInput
New in Django 1.0: Please, see the release notes

Date/time input as a simple text box: <input type='text' ...>

Takes one optional argument:

<!-- [django.forms.DateTimeInput.format]-->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' .

<!-- [django.forms.TimeInput]-->class TimeInput

Time input as a simple text box: <input type='text' ...>

Takes one optional argument:

<!-- [django.forms.TimeInput.format]-->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.
<!-- [django.forms.Textarea]-->class Textarea
Text area: <textarea>...</textarea>
<!-- [django.forms.CheckboxInput]-->class CheckboxInput

Checkbox: <input type='checkbox' ...>

Takes one optional argument:

<!-- [django.forms.CheckboxInput.check_test]-->check_test
A callable that takes the value of the CheckBoxInput and returns True if the checkbox should be checked for that value.
<!-- [django.forms.Select]-->class Select

Select widget: <select><option ...>...</select>

Requires that your field provides choices .

<!-- [django.forms.NullBooleanSelect]-->class NullBooleanSelect
Select widget with options ‘Unknown’, ‘Yes’ and ‘No’
<!-- [django.forms.SelectMultiple]-->class SelectMultiple

Select widget allowing multiple selection: <select multiple='multiple'>...</select>

Requires that your field provides choices .

<!-- [django.forms.RadioSelect]-->class RadioSelect

A list of radio buttons:

<ul>
  <li><input type='radio' ...></li>
  ...
</ul>

Requires that your field provides choices .

<!-- [django.forms.CheckboxSelectMultiple]-->class CheckboxSelectMultiple

A list of checkboxes:

<ul>
  <li><input type='checkbox' ...></li>
  ...
</ul>
<!-- [django.forms.MultiWidget]-->class MultiWidget
Wrapper around multiple other widgets
<!-- [django.forms.SplitDateTimeWidget]-->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.
<!-- [django.forms.SelectDateWidget]-->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

<!-- [django.forms.Form.widget]-->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
()

<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:

<!-- [django.forms.Widget.attrs]-->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
()

<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>

分享到:
评论

相关推荐

    PyPI 官网下载 | django_widgets-0.1.15.tar.gz

    **PyPI 官网下载 | django_widgets-0.1.15.tar.gz** 在Python的世界里,`PyPI`(Python Package Index)是官方的第三方软件包仓库,它为Python开发者提供了一个集中化的地方来发布、查找和安装软件包。这个资源`...

    django表单的Widgets使用详解

    本文将深入探讨Django表单的Widgets的使用,以及如何自定义它们的样式和参数。 首先,理解Widget与字段(fields)的区别至关重要。字段负责数据的验证和模板中的呈现,而Widget则关注HTML表单元素的渲染。每个字段...

    django-checkboxselectmultiple:使用多个复选框而不是 Django 小部件

    django-checkboxselectmultiple 使用多个复选框代替&lt;select multiple&gt; Django 小部件。 使用它而不是 Django 的默认值的好处是更好地与 Django Admin 集成。 Javascript 基于 Stefano Contini 的代码 安装 ...

    PyPI 官网下载 | django_map_widgets-0.3.0-py3-none-any.whl

    **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库 | django-ui-widgets-1.1.6.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:django-ui-widgets-1.1.6.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    django-map-widgets:Django Postgis字段的可插入地图小部件

    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库。资源全名:django-ui-widgets-1.2b0.tar.gz

    django layui表单加图片上传功能(Django,layui).zip

    widgets = { 'image': forms.ClearableFileInput(), } ``` 4. **Layui前端页面**: 在HTML模板文件(如`upload_image.html`)中,使用Layui的组件创建上传表单。Layui的`upload`模块可以帮助我们实现图片预览...

    Python库 | django-social-widgets-0.1.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:django-social-widgets-0.1.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    django项目添加了fckeditor

    为了在表单中显示FCKeditor,你需要创建一个自定义表单或者使用Django的ModelForm,并在表单的Meta类中指定`widgets`,将`FCKEditorWidget`用于`content`字段: ```python from django import forms from .models ...

    django1.6.5 form/formset/inline formset

    Django是一个高级Python Web框架,它鼓励快速开发和干净、实用的设计。它具有许多功能,可以处理数据密集型的网站,无需花费太多的时间。在Web开发中,表单是数据交互的基本组成部分,Django为此提供了丰富的支持。 ...

    Python Frameworks Web 2.0 Programming with Django and TurboGears

    - **第6章:Widgets**:讲解TurboGears提供的Widgets组件库,帮助开发者轻松创建丰富的用户界面。 - **第7章:高级TurboGears主题**:介绍一些高级特性,如自定义模板引擎、国际化支持等。 5. **Django部分**: ...

    django_material_widgets:使用Web的Material Components样式化的Django小部件

    **django_material_widgets:使用Web的Material Components样式化的Django小部件** `django_material_widgets` 是一个优秀的Django应用,它允许开发者将Google的Material Design组件引入到Django表单和模型小部件中...

    Django富文本实测

    widgets = { 'content': forms.Textarea(attrs={'class': 'tinymce'}), } ``` 在这里,我们使用了`forms.Textarea`并添加了一个`tinymce`类,这将在前端触发TinyMCE的初始化。 现在,我们需要在视图`views.py`...

    django-docs-2.2-zh-hans.zip

    基础: 概览 | 表单 API | 内建字段 | 内建 widgets 进阶: 针对模型的表单 | 整合媒体 | 表单集 | 自定义验证 开发进程 学习众多的组件及工具,来帮助你开发和测试 Django 应用: 设置: 概览 | 完整的设置列表 应用...

    Python库 | django_widgets-0.1.11.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:django_widgets-0.1.11.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    Professional.Python.Frameworks.Web.2.0.Programming.with.Django.and.TurboGears

    第4章至第7章深入探讨了TurboGears的核心特性和高级话题,如用户身份验证、访问者追踪、小部件(widgets)的使用以及框架的高级特性,使读者能够熟练掌握TurboGears的使用方法。 ### 高级客户端主题 除了服务器端...

Global site tag (gtag.js) - Google Analytics