We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nothing is impossible for a willing heart.
Django Blog
Django Tips
django.forms
HTML
<Form>
Form
views.py
app
forms.py
Field
required=False
from djanog import forms class EmailPostForm(forms.Form): name = forms.CharField(max_length=25) email = forms.EmailField() to = forms.EmailField() comments = forms.CharField(required=False, widget=forms.Textarea)
forms
widget
widget=forms.Textarea
检验
显示
comments
text
Textarea
f.is_bound
f
f.is_valid()
f.errors
f.cleaned_data
f.as_p()
<p>...</p>
f.as_ul()
f.as_table()
<table>
<ul>
<table> {{ f.as_table }} </table>
initial
f = EmailPostForm(initial={'to': 'xxxx@qq.com'})
errors
request.POST
clean_ + 字段名
CharField
字段内容 = self.cleaned_data[字段名]
raise forms.ValidationError('Not enough words')
return
form
e_mail
E mail
label
class EmailPostForm(forms.Form): email = forms.EmailField(required=False, label='Your e-mail address')
URLconf urls.py
urlpatterns
url
import
views
app.views.xxx
URL
(?P<name>pattern)
name
from django.conf.urls import url from . import views urlpattrens = [ url(r'(?P<year>\d{4})/(?P<month>\d{2})/$', views.post_detail), ]
URLconfs
BUG
The text was updated successfully, but these errors were encountered:
No branches or pull requests
0x01 Django
Django Blog
学习总结Django Tips
django.forms
库HTML
的<Form>
定义一个Form
类,这个类可以存在任何地方,甚至可以直接卸载views.py
中,但是一般都会在app
目录下新建一个forms.py
文件,所有Form
类都放在其中Form
类的属性,被展现成Field
类,每一个字段都默认是必填的,如果要使某个字段是可选项,必须指定required=False
forms
框架把每个字段的显示逻辑分离到一组部件(widget
)中,每个字段类型都拥有一个默认的部件,比如:利用widget=forms.Textarea
就可以替换掉默认的部件Field
类表现的是检验
逻辑,而部件表现的是显示
逻辑comments
需要可以多行文本,而默认的text
是单行,所以显示逻辑需要多行,部件就可以更换成Textarea
Form
对象有以下属性:f.is_bound
: 判断f
是否绑定表单类f.is_valid()
: 判断f
(已绑定表单类)中的数据是否合法f.errors
: 提供一个字段和错误消息相映射的字典(没有的话,对应空字符串)f.cleaned_data
:Form
实体f
中的数据合法,会有这个属性,返回一个提交数据的字典f.as_p()
: 用<p>...</p>
标签来为每个字段添加格式输出,类似的有f.as_ul()
f.as_table()
<table>
<ul>
标签添加时,外面任然需要写,比如Form
实体时,可以使用initial
参数来实现给字段添加初始值errors
反馈(即返回的是空字符串),而绑定的表单(即不通过初始值传入数据,比如用request.POST
传入)就会有errors
反馈forms.py
中可以自定义字段的检测标准,在forms
类中创建检测函数clean_ + 字段名
为函数名,系统会自动寻找匹配函数的检测方法,如果检测到存在这样的函数,那么这个检测函数将在指定字段的默认检测逻辑执行之后被调用(也就是先检测是否是CharField
之类的类型)字段内容 = self.cleaned_data[字段名]
来获取,最后检测完后raise forms.ValidationError('Not enough words')
抛出异常return
返回 字段内容forms.py
中定义form
类中的字段时,可以自定义字段名在HTML
表单中生成的标签HTML
中生成标签名 : 空格代替下划线,首字母大写,如e_mail
就是E mail
label
参数URLconf urls.py
要点urlpatterns
中导入url
有多种方法import
导入views
views
,直接写app.views.xxx
(直接写入的话,需要用引号包裹)URL
部分加上圆括号,表示将捕获的文本作为位置参数传递给视图函数调用(?P<name>pattern)
name
就是组的名字URLconfs
更加清晰,减少BUG
The text was updated successfully, but these errors were encountered: