django框架forms组件用法实例详解


Posted in Python onDecember 10, 2019

本文实例讲述了django框架forms组件用法。分享给大家供大家参考,具体如下:

在django中forms组件有其强大的功能,里面集合和众多的函数和方法:下面来看一下它的源码

"""
Form classes
"""

from __future__ import unicode_literals

import copy
from collections import OrderedDict

from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
# BoundField is imported for backwards compatibility in Django 1.9
from django.forms.boundfield import BoundField # NOQA
from django.forms.fields import Field, FileField
# pretty_name is imported for backwards compatibility in Django 1.9
from django.forms.utils import ErrorDict, ErrorList, pretty_name # NOQA
from django.forms.widgets import Media, MediaDefiningClass
from django.utils import six
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils.functional import cached_property
from django.utils.html import conditional_escape, html_safe
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _

from .renderers import get_default_renderer

__all__ = ('BaseForm', 'Form')


class DeclarativeFieldsMetaclass(MediaDefiningClass):
  """
  Metaclass that collects Fields declared on the base classes.
  """
  def __new__(mcs, name, bases, attrs):
    # Collect fields from current class.
    current_fields = []
    for key, value in list(attrs.items()):
      if isinstance(value, Field):
        current_fields.append((key, value))
        attrs.pop(key)
    current_fields.sort(key=lambda x: x[1].creation_counter)
    attrs['declared_fields'] = OrderedDict(current_fields)

    new_class = super(DeclarativeFieldsMetaclass, mcs).__new__(mcs, name, bases, attrs)

    # Walk through the MRO.
    declared_fields = OrderedDict()
    for base in reversed(new_class.__mro__):
      # Collect fields from base class.
      if hasattr(base, 'declared_fields'):
        declared_fields.update(base.declared_fields)

      # Field shadowing.
      for attr, value in base.__dict__.items():
        if value is None and attr in declared_fields:
          declared_fields.pop(attr)

    new_class.base_fields = declared_fields
    new_class.declared_fields = declared_fields

    return new_class


@html_safe
@python_2_unicode_compatible
class BaseForm(object):
  # This is the main implementation of all the Form logic. Note that this
  # class is different than Form. See the comments by the Form class for more
  # information. Any improvements to the form API should be made to *this*
  # class, not to the Form class.
  default_renderer = None
  field_order = None
  prefix = None
  use_required_attribute = True

  def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
         initial=None, error_class=ErrorList, label_suffix=None,
         empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None):
    self.is_bound = data is not None or files is not None
    self.data = data or {}
    self.files = files or {}
    self.auto_id = auto_id
    if prefix is not None:
      self.prefix = prefix
    self.initial = initial or {}
    self.error_class = error_class
    # Translators: This is the default suffix added to form field labels
    self.label_suffix = label_suffix if label_suffix is not None else _(':')
    self.empty_permitted = empty_permitted
    self._errors = None # Stores the errors after clean() has been called.

    # The base_fields class attribute is the *class-wide* definition of
    # fields. Because a particular *instance* of the class might want to
    # alter self.fields, we create self.fields here by copying base_fields.
    # Instances should always modify self.fields; they should not modify
    # self.base_fields.
    self.fields = copy.deepcopy(self.base_fields)
    self._bound_fields_cache = {}
    self.order_fields(self.field_order if field_order is None else field_order)

    if use_required_attribute is not None:
      self.use_required_attribute = use_required_attribute

    # Initialize form renderer. Use a global default if not specified
    # either as an argument or as self.default_renderer.
    if renderer is None:
      if self.default_renderer is None:
        renderer = get_default_renderer()
      else:
        renderer = self.default_renderer
        if isinstance(self.default_renderer, type):
          renderer = renderer()
    self.renderer = renderer

  def order_fields(self, field_order):
    """
    Rearranges the fields according to field_order.

    field_order is a list of field names specifying the order. Fields not
    included in the list are appended in the default order for backward
    compatibility with subclasses not overriding field_order. If field_order
    is None, all fields are kept in the order defined in the class.
    Unknown fields in field_order are ignored to allow disabling fields in
    form subclasses without redefining ordering.
    """
    if field_order is None:
      return
    fields = OrderedDict()
    for key in field_order:
      try:
        fields[key] = self.fields.pop(key)
      except KeyError: # ignore unknown fields
        pass
    fields.update(self.fields) # add remaining fields in original order
    self.fields = fields

  def __str__(self):
    return self.as_table()

  def __repr__(self):
    if self._errors is None:
      is_valid = "Unknown"
    else:
      is_valid = self.is_bound and not bool(self._errors)
    return '<%(cls)s bound=%(bound)s, valid=%(valid)s, fields=(%(fields)s)>' % {
      'cls': self.__class__.__name__,
      'bound': self.is_bound,
      'valid': is_valid,
      'fields': ';'.join(self.fields),
    }

  def __iter__(self):
    for name in self.fields:
      yield self[name]

  def __getitem__(self, name):
    "Returns a BoundField with the given name."
    try:
      field = self.fields[name]
    except KeyError:
      raise KeyError(
        "Key '%s' not found in '%s'. Choices are: %s." % (
          name,
          self.__class__.__name__,
          ', '.join(sorted(f for f in self.fields)),
        )
      )
    if name not in self._bound_fields_cache:
      self._bound_fields_cache[name] = field.get_bound_field(self, name)
    return self._bound_fields_cache[name]

  @property
  def errors(self):
    "Returns an ErrorDict for the data provided for the form"
    if self._errors is None:
      self.full_clean()
    return self._errors

  def is_valid(self):
    """
    Returns True if the form has no errors. Otherwise, False. If errors are
    being ignored, returns False.
    """
    return self.is_bound and not self.errors

  def add_prefix(self, field_name):
    """
    Returns the field name with a prefix appended, if this Form has a
    prefix set.

    Subclasses may wish to override.
    """
    return '%s-%s' % (self.prefix, field_name) if self.prefix else field_name

  def add_initial_prefix(self, field_name):
    """
    Add a 'initial' prefix for checking dynamic initial values
    """
    return 'initial-%s' % self.add_prefix(field_name)

  def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
    "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
    top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
    output, hidden_fields = [], []

    for name, field in self.fields.items():
      html_class_attr = ''
      bf = self[name]
      # Escape and cache in local variable.
      bf_errors = self.error_class([conditional_escape(error) for error in bf.errors])
      if bf.is_hidden:
        if bf_errors:
          top_errors.extend(
            [_('(Hidden field %(name)s) %(error)s') % {'name': name, 'error': force_text(e)}
             for e in bf_errors])
        hidden_fields.append(six.text_type(bf))
      else:
        # Create a 'class="..."' attribute if the row should have any
        # CSS classes applied.
        css_classes = bf.css_classes()
        if css_classes:
          html_class_attr = ' class="%s"' % css_classes

        if errors_on_separate_row and bf_errors:
          output.append(error_row % force_text(bf_errors))

        if bf.label:
          label = conditional_escape(force_text(bf.label))
          label = bf.label_tag(label) or ''
        else:
          label = ''

        if field.help_text:
          help_text = help_text_html % force_text(field.help_text)
        else:
          help_text = ''

        output.append(normal_row % {
          'errors': force_text(bf_errors),
          'label': force_text(label),
          'field': six.text_type(bf),
          'help_text': help_text,
          'html_class_attr': html_class_attr,
          'css_classes': css_classes,
          'field_name': bf.html_name,
        })

    if top_errors:
      output.insert(0, error_row % force_text(top_errors))

    if hidden_fields: # Insert any hidden fields in the last row.
      str_hidden = ''.join(hidden_fields)
      if output:
        last_row = output[-1]
        # Chop off the trailing row_ender (e.g. '</td></tr>') and
        # insert the hidden fields.
        if not last_row.endswith(row_ender):
          # This can happen in the as_p() case (and possibly others
          # that users write): if there are only top errors, we may
          # not be able to conscript the last row for our purposes,
          # so insert a new, empty row.
          last_row = (normal_row % {
            'errors': '',
            'label': '',
            'field': '',
            'help_text': '',
            'html_class_attr': html_class_attr,
            'css_classes': '',
            'field_name': '',
          })
          output.append(last_row)
        output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
      else:
        # If there aren't any rows in the output, just append the
        # hidden fields.
        output.append(str_hidden)
    return mark_safe('\n'.join(output))

  def as_table(self):
    "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
    return self._html_output(
      normal_row='<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
      error_row='<tr><td colspan="2">%s</td></tr>',
      row_ender='</td></tr>',
      help_text_html='<br /><span class="helptext">%s</span>',
      errors_on_separate_row=False)

  def as_ul(self):
    "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
    return self._html_output(
      normal_row='<li%(html_class_attr)s>%(errors)s%(label)s %(field)s%(help_text)s</li>',
      error_row='<li>%s</li>',
      row_ender='</li>',
      help_text_html=' <span class="helptext">%s</span>',
      errors_on_separate_row=False)

  def as_p(self):
    "Returns this form rendered as HTML <p>s."
    return self._html_output(
      normal_row='<p%(html_class_attr)s>%(label)s %(field)s%(help_text)s</p>',
      error_row='%s',
      row_ender='</p>',
      help_text_html=' <span class="helptext">%s</span>',
      errors_on_separate_row=True)

  def non_field_errors(self):
    """
    Returns an ErrorList of errors that aren't associated with a particular
    field -- i.e., from Form.clean(). Returns an empty ErrorList if there
    are none.
    """
    return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))

  def add_error(self, field, error):
    """
    Update the content of `self._errors`.

    The `field` argument is the name of the field to which the errors
    should be added. If its value is None the errors will be treated as
    NON_FIELD_ERRORS.

    The `error` argument can be a single error, a list of errors, or a
    dictionary that maps field names to lists of errors. What we define as
    an "error" can be either a simple string or an instance of
    ValidationError with its message attribute set and what we define as
    list or dictionary can be an actual `list` or `dict` or an instance
    of ValidationError with its `error_list` or `error_dict` attribute set.

    If `error` is a dictionary, the `field` argument *must* be None and
    errors will be added to the fields that correspond to the keys of the
    dictionary.
    """
    if not isinstance(error, ValidationError):
      # Normalize to ValidationError and let its constructor
      # do the hard work of making sense of the input.
      error = ValidationError(error)

    if hasattr(error, 'error_dict'):
      if field is not None:
        raise TypeError(
          "The argument `field` must be `None` when the `error` "
          "argument contains errors for multiple fields."
        )
      else:
        error = error.error_dict
    else:
      error = {field or NON_FIELD_ERRORS: error.error_list}

    for field, error_list in error.items():
      if field not in self.errors:
        if field != NON_FIELD_ERRORS and field not in self.fields:
          raise ValueError(
            "'%s' has no field named '%s'." % (self.__class__.__name__, field))
        if field == NON_FIELD_ERRORS:
          self._errors[field] = self.error_class(error_class='nonfield')
        else:
          self._errors[field] = self.error_class()
      self._errors[field].extend(error_list)
      if field in self.cleaned_data:
        del self.cleaned_data[field]

  def has_error(self, field, code=None):
    if code is None:
      return field in self.errors
    if field in self.errors:
      for error in self.errors.as_data()[field]:
        if error.code == code:
          return True
    return False

  def full_clean(self):
    """
    Cleans all of self.data and populates self._errors and
    self.cleaned_data.
    """
    self._errors = ErrorDict()
    if not self.is_bound: # Stop further processing.
      return
    self.cleaned_data = {}
    # If the form is permitted to be empty, and none of the form data has
    # changed from the initial data, short circuit any validation.
    if self.empty_permitted and not self.has_changed():
      return

    self._clean_fields()
    self._clean_form()
    self._post_clean()

  def _clean_fields(self):
    for name, field in self.fields.items():
      # value_from_datadict() gets the data from the data dictionaries.
      # Each widget type knows how to retrieve its own data, because some
      # widgets split data over several HTML fields.
      if field.disabled:
        value = self.get_initial_for_field(field, name)
      else:
        value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
      try:
        if isinstance(field, FileField):
          initial = self.get_initial_for_field(field, name)
          value = field.clean(value, initial)
        else:
          value = field.clean(value)
        self.cleaned_data[name] = value
        if hasattr(self, 'clean_%s' % name):
          value = getattr(self, 'clean_%s' % name)()
          self.cleaned_data[name] = value
      except ValidationError as e:
        self.add_error(name, e)

  def _clean_form(self):
    try:
      cleaned_data = self.clean()
    except ValidationError as e:
      self.add_error(None, e)
    else:
      if cleaned_data is not None:
        self.cleaned_data = cleaned_data

  def _post_clean(self):
    """
    An internal hook for performing additional cleaning after form cleaning
    is complete. Used for model validation in model forms.
    """
    pass

  def clean(self):
    """
    Hook for doing any extra form-wide cleaning after Field.clean() has been
    called on every field. Any ValidationError raised by this method will
    not be associated with a particular field; it will have a special-case
    association with the field named '__all__'.
    """
    return self.cleaned_data

  def has_changed(self):
    """
    Returns True if data differs from initial.
    """
    return bool(self.changed_data)

  @cached_property
  def changed_data(self):
    data = []
    for name, field in self.fields.items():
      prefixed_name = self.add_prefix(name)
      data_value = field.widget.value_from_datadict(self.data, self.files, prefixed_name)
      if not field.show_hidden_initial:
        # Use the BoundField's initial as this is the value passed to
        # the widget.
        initial_value = self[name].initial
      else:
        initial_prefixed_name = self.add_initial_prefix(name)
        hidden_widget = field.hidden_widget()
        try:
          initial_value = field.to_python(hidden_widget.value_from_datadict(
            self.data, self.files, initial_prefixed_name))
        except ValidationError:
          # Always assume data has changed if validation fails.
          data.append(name)
          continue
      if field.has_changed(initial_value, data_value):
        data.append(name)
    return data

  @property
  def media(self):
    """
    Provide a description of all media required to render the widgets on this form
    """
    media = Media()
    for field in self.fields.values():
      media = media + field.widget.media
    return media

  def is_multipart(self):
    """
    Returns True if the form needs to be multipart-encoded, i.e. it has
    FileInput. Otherwise, False.
    """
    for field in self.fields.values():
      if field.widget.needs_multipart_form:
        return True
    return False

  def hidden_fields(self):
    """
    Returns a list of all the BoundField objects that are hidden fields.
    Useful for manual form layout in templates.
    """
    return [field for field in self if field.is_hidden]

  def visible_fields(self):
    """
    Returns a list of BoundField objects that aren't hidden fields.
    The opposite of the hidden_fields() method.
    """
    return [field for field in self if not field.is_hidden]

  def get_initial_for_field(self, field, field_name):
    """
    Return initial data for field on form. Use initial data from the form
    or the field, in that order. Evaluate callable values.
    """
    value = self.initial.get(field_name, field.initial)
    if callable(value):
      value = value()
    return value


class Form(six.with_metaclass(DeclarativeFieldsMetaclass, BaseForm)):
  "A collection of Fields, plus their associated data."
  # This is a separate class from BaseForm in order to abstract the way
  # self.fields is specified. This class (Form) is the one that does the
  # fancy metaclass stuff purely for the semantic sugar -- it allows one
  # to define a form using declarative syntax.
  # BaseForm itself has no way of designating self.fields.

抛开其他的代码,我们之关注我们需要的东西

1 首先数据的验证通过的是is_vaild方法,它下面有以下东西

return self.is_bound and not self.errors  对照以下也就是is_bound不能为空,并且 errors方法返回的为空,则通过验证

2 我们找到 errors 方法,

@property
  def errors(self):
    "Returns an ErrorDict for the data provided for the form"
    if self._errors is None:
      self.full_clean() #找到full_clean
    return self._errors

3 找到 full_clean 方法

def full_clean(self):
    """
    Cleans all of self.data and populates self._errors and
    self.cleaned_data.
    """
    self._errors = ErrorDict()  #先赋一个空的错误字典
    if not self.is_bound: # Stop further processing.
      return
    self.cleaned_data = {}    #定义一个空的cleaned_data字典
    # If the form is permitted to be empty, and none of the form data has
    # changed from the initial data, short circuit any validation.
    if self.empty_permitted and not self.has_changed():
      return
#执行下面三种方法
    self._clean_fields() 
    self._clean_form()
    self._post_clean()

4  找到 _clean_fields 方法

def _clean_fields(self):
    for name, field in self.fields.items(): #依次遍历字段
      # value_from_datadict() gets the data from the data dictionaries.
      # Each widget type knows how to retrieve its own data, because some
      # widgets split data over several HTML fields.
      if field.disabled:
        value = self.get_initial_for_field(field, name)
      else:
        value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
      try:
        if isinstance(field, FileField):
          initial = self.get_initial_for_field(field, name)
          value = field.clean(value, initial)
        else:
          value = field.clean(value)
        self.cleaned_data[name] = value  #通过自定义和django里面定义的方法后,将它加入到 cleand_data字典中
        if hasattr(self, 'clean_%s' % name): #判断是否有自定义的局部钩子函数
          value = getattr(self, 'clean_%s' % name)() #通过了局部钩子则返回数据
          self.cleaned_data[name] = value  #将数据加入 cleaned_data
      except ValidationError as e:
        self.add_error(name, e) #错误的话执行 add_error

5 找到add_error

self._errors[field].extend(error_list) #将错误信息加入 _errors
      if field in self.cleaned_data:
        del self.cleaned_data[field] #删除cleaned_data中这条数据,之前是通过自定义方法加入的,通不过 钩子函数

6 回到上面执行  self._clean_form()

def _clean_form(self):
    try:
      cleaned_data = self.clean() #接着执行 clean()全局钩子
    except ValidationError as e:
      self.add_error(None, e)
    else:
      if cleaned_data is not None:
        self.cleaned_data = cleaned_data

7接着查看 全局钩子  clean ,它只有一个返回的数据,因此我们可以自定义一个

def clean(self):
    return self.cleaned_data #注意返回的数据和局部钩子的区别

 8 最后查看 _post_clean ,说白了就是什么都没有

def _post_clean(self):
    pass

以上是源码内部执行的顺序,下面我们来看一个例子

 1 自定义判断字段

django框架forms组件用法实例详解

2 自定义局部钩子

django框架forms组件用法实例详解

3自定义全局钩子

django框架forms组件用法实例详解

4 继承并使用

django框架forms组件用法实例详解

5 .1在模板中显示一

django框架forms组件用法实例详解

 上面的代码 form_obj.errors.all_error  也可以改成  {{ form_obj.non_field_errors.0 }}

5.2 在模板中显示二

django框架forms组件用法实例详解

6在浏览器上面看效果

django框架forms组件用法实例详解

其他:

我们在forms组件中处理的不同情况

django框架forms组件用法实例详解

希望本文所述对大家基于Django框架的Python程序设计有所帮助。

Python 相关文章推荐
python使用rabbitmq实现网络爬虫示例
Feb 20 Python
详解Python的Django框架中的中间件
Jul 24 Python
Python制作爬虫采集小说
Oct 25 Python
关于Django外键赋值问题详解
Aug 13 Python
Python实现图片转字符画的示例代码
Aug 21 Python
python OpenCV学习笔记之绘制直方图的方法
Feb 08 Python
用TensorFlow实现戴明回归算法的示例
May 02 Python
详解Django中间件的5种自定义方法
Jul 26 Python
Python 输出时去掉列表元组外面的方括号与圆括号的方法
Dec 24 Python
python 处理telnet返回的More,以及get想要的那个参数方法
Feb 14 Python
Python计算公交发车时间的完整代码
Feb 12 Python
如何将Pycharm中调整字体大小的方式设置为&quot;ctrl+鼠标滚轮上下滑&quot;
Nov 17 Python
django框架auth模块用法实例详解
Dec 10 #Python
django框架中间件原理与用法详解
Dec 10 #Python
Django2 连接MySQL及model测试实例分析
Dec 10 #Python
python实现将视频按帧读取到自定义目录
Dec 10 #Python
django连接mysql数据库及建表操作实例详解
Dec 10 #Python
python 通过视频url获取视频的宽高方式
Dec 10 #Python
Python imageio读取视频并进行编解码详解
Dec 10 #Python
You might like
不用GD库生成当前时间的PNG格式图象的程序
2006/10/09 PHP
PHP如何解决网站大流量与高并发的问题
2011/06/25 PHP
详解php设置session(过期、失效、有效期)
2015/11/12 PHP
PHP编程计算文件或数组中单词出现频率的方法
2017/05/22 PHP
laravel实现图片上传预览,及编辑时可更换图片,并实时变化的例子
2019/11/14 PHP
分享别人写的一个小型js框架
2007/08/13 Javascript
IE下使用cloneNode注意事项分享
2012/11/22 Javascript
js中top的作用深入剖析
2014/03/04 Javascript
鼠标左键单击冲突的问题解决方法(防止冒泡)
2014/05/14 Javascript
JavaScript实现在数组中查找不同顺序排列的字符串
2014/09/26 Javascript
上传文件返回的json数据会被提示下载问题解决方案
2014/12/03 Javascript
IE6-IE9使用JSON、table.innerHTML所引发的问题
2015/12/22 Javascript
JavaScript字符串删除重复字符的方法
2015/12/25 Javascript
jQuery获取attr()与prop()属性值的方法及区别介绍
2016/07/06 Javascript
详解AngularJs路由之Ui-router-resolve(预加载)
2017/06/13 Javascript
详解如何用模块化的方式写vuejs
2017/12/16 Javascript
jQuery解析json格式数据示例
2018/09/01 jQuery
iView-admin 动态路由问题的解决方法
2018/10/03 Javascript
node.js爬取中关村的在线电瓶车信息
2018/11/13 Javascript
layui 地区三级联动 form select 渲染的实例
2019/09/27 Javascript
python实现判断一个字符串是否是合法IP地址的示例
2018/06/04 Python
python学生管理系统
2019/01/30 Python
使用Python将Mysql的查询数据导出到文件的方法
2019/02/25 Python
Python发起请求提示UnicodeEncodeError错误代码解决方法
2020/04/21 Python
全球领先美式家具品牌:Ashley爱室丽家居
2017/08/07 全球购物
美国半成品食材配送服务商:Home Chef
2018/01/25 全球购物
香港万宁官方海外旗舰店:香港健与美连锁店
2018/09/27 全球购物
德国婴儿服装和婴儿用品购买网站:Baby Sweets
2019/12/08 全球购物
解决python 输出到csv 出现多空行的情况
2021/03/24 Python
小小的船教学反思
2014/02/21 职场文书
幼儿园父亲节活动方案
2014/03/11 职场文书
工地宣传标语
2014/06/18 职场文书
社保代办委托书怎么写
2014/10/06 职场文书
详解Python为什么不用设计模式
2021/06/24 Python
MySQL和Oracle批量插入SQL的通用写法示例
2021/11/17 MySQL
详解MySQL的内连接和外连接
2023/05/08 MySQL