Django Form'initial'和'bound数据'之间的区别? [英] Difference between Django Form 'initial' and 'bound data'?

查看:590
本文介绍了Django Form'initial'和'bound数据'之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个这样的例子:

  class MyForm(forms.Form):
name = forms.CharField ()

我试图抓住以下两个片段之间的区别是:



绑定数据样式:

  my_form = MyForm({'name' :request.user.first_name})

初始数据样式:

  my_form = MyForm(initial = {'name':request.user.first_name})

该文档似乎建议不是初始化是动态初始值,而是能够将绑定数据传递给构造函数完成完全相同的事情。我以前使用过初始数据的动态值,但我很想使用更直接的绑定数据样式,但是想了解这两种风格之间的真正区别。

解决方案

这是django文档关于绑定和未绑定表单


表单实例是绑定到一组数据,或未绑定




  • 如果它与一组数据绑定,则它能够验证该数据并将表单呈现为HTML中显示的数据。

  • 如果未绑定,则无法进行验证(因为没有数据可以验证!),但它仍然可以将空白表单呈现为HTML。 >

您不能真正看到您给出的示例表格的区别,因为表单在绑定数据风格。我们通过添加一个年龄字段来扩展表单,然后差异将更加明显。

  class MyForm(forms.Form):
name = forms.CharField()
age = forms.IntegerField()



绑定形式



  my_form = MyForm({'name':request .user.first_name})

此表单无效,因为 age 未指定。当您在模板中呈现表单时,您将看到年龄字段的验证错误。



动态初始数据



  my_form = MyForm(initial = {'name':request.user.first_name})

此表单未绑定。验证不被触发,所以渲染模板时不会显示错误。


Given an example like this:

class MyForm(forms.Form): 
    name = forms.CharField()

I'm trying to grasp what the difference between the following two snippets is:

"Bound Data" style:

my_form = MyForm({'name': request.user.first_name})

"Initial data" style:

my_form = MyForm(initial={'name': request.user.first_name})

The documentation seems to suggest than "initial is for dynamic initial values", and yet being able to pass "bound data" to the constructor accomplishes exactly the same thing. I've used initial data in the past for dynamic values, but I'm tempted to use the more straightforward "bound data" style, but would like some insights about what the real difference between these two styles is.

解决方案

Here's the key part from the django docs on bound and unbound forms.

A Form instance is either bound to a set of data, or unbound:

  • If it’s bound to a set of data, it’s capable of validating that data and rendering the form as HTML with the data displayed in the HTML.
  • If it’s unbound, it cannot do validation (because there’s no data to validate!), but it can still render the blank form as HTML.

You can't really see the difference for the example form you gave, because the form is valid in the "bound data" style. Let's extend the form by adding an age field, then the difference will be more obvious.

class MyForm(forms.Form):
    name = forms.CharField()
    age = forms.IntegerField()

Bound form

my_form = MyForm({'name': request.user.first_name})

This form is invalid, because age is not specified. When you render the form in the template, you will see validation errors for the age field.

Unbound form with dynamic initial data

my_form = MyForm(initial={'name':request.user.first_name})

This form is unbound. Validation is not triggered, so there will not be any errors displayed when you render the template.

这篇关于Django Form'initial'和'bound数据'之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆