django-registration 中的 Django 字段
我正在设置 django 注册,我在 RegistrationForm 中遇到了这段代码 --
attrs_dict = { 'class': 'required' }
email = forms.EmailField(widget=forms.TextInput
(attrs=dict(attrs_dict, maxlength=75)),
label='Email')
(attrs=dict(attrs_dict, maxlength=75))
部分的含义/作用是什么?我知道 maxlength 部分的作用,但不清楚字典的创建在做什么,以及 attrs_dict 正在做什么。对这段代码的任何解释都会很棒。谢谢。
I am setting up django registration, and I came across this piece of code in the RegistrationForm --
attrs_dict = { 'class': 'required' }
email = forms.EmailField(widget=forms.TextInput
(attrs=dict(attrs_dict, maxlength=75)),
label='Email')
What does the part (attrs=dict(attrs_dict, maxlength=75))
mean/do? I know what the maxlength
part does but was unclear what the creation of a dictionary is doing, and what the attrs_dict
is doing. Any explanation of this piece of code would be great. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些测试表明 dict(attr_dict, maxlenght=75) 等于
So当电子邮件字段呈现为 html 元素时,2 个属性(class 和 maxlength)将添加到标签中。
A bit of test showed that dict(attr_dict, maxlenght=75) equals to
So when the email filed is rendered to an html element, 2 attributes, class and maxlength will be added to the label.
它正在创建一个属性字典,需要在最终呈现的形式中添加验证类型的内容,这样我们就不需要在模板代码中执行任何操作来添加验证和安全性。
It is creating a dictionary of attributes which will be required to add validation kind of things in finally rendered form, in this way we dont need to do anything in the template code to add validation and security.
django 中的每个表单字段都使用一个小部件。您可以在字段创建期间指定它,也可以使用默认小部件。
在这里,您在
EmailField
上指定小部件TextInput
(attrs=dict(attrs_dict, maxlength=75)) 变为:
现在这些将作为该小部件渲染的 html 中的属性出现。因此,字段
email
的渲染 html 看起来像:Each form field in django uses a widget. You can either specify it during field creation or a default widget is used.
Here you are specifying widget
TextInput
onEmailField
(attrs=dict(attrs_dict, maxlength=75)) becomes:
Now these will be present as attributes in the rendered html for this widget. So, rendered html for field
email
would look like: