如何正确使用 Django Forms
2个回答
2016-02-29 · 知道合伙人软件行家
关注
展开全部
首先你要创建自己的Form类,继承于django的Form,
然后创建form中的字段,例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from django import forms
class AuthenticationForm(forms.Form):
"""
Base class for authenticating users. Extend this to get a form that accepts
username/password logins.
"""
username = forms.CharField(label=_("Username"), max_length=254)
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
error_messages = {
'invalid_login': _("Please enter a correct username and password. "
"Note that both fields may be case-sensitive."),
'no_cookies': _("Your Web browser doesn't appear to have cookies "
"enabled. Cookies are required for logging in."),
'inactive': _("This account is inactive."),
}
如果你英文还可以,可以直接看官方文档,最权威的:
https://docs.djangoproject.com/en/1.7/topics/forms/
然后创建form中的字段,例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from django import forms
class AuthenticationForm(forms.Form):
"""
Base class for authenticating users. Extend this to get a form that accepts
username/password logins.
"""
username = forms.CharField(label=_("Username"), max_length=254)
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
error_messages = {
'invalid_login': _("Please enter a correct username and password. "
"Note that both fields may be case-sensitive."),
'no_cookies': _("Your Web browser doesn't appear to have cookies "
"enabled. Cookies are required for logging in."),
'inactive': _("This account is inactive."),
}
如果你英文还可以,可以直接看官方文档,最权威的:
https://docs.djangoproject.com/en/1.7/topics/forms/
2016-04-07 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
1.定义form
class CustomerForm(ModelForm):
class Meta:
model = Customer
exclude = ('User','adder','addtime', 'adderip','status')
2.使用form
form = CustomerForm(request.POST)
if form.is_valid():
form.save()
3.显示
<article>
<span>{{ f.label_tag }} :</span>
{{ f }}
<span class="help">{{ f.help_text }}</span>
<span class="error">{{ f.errors.as_text }}</span>
</article>
class CustomerForm(ModelForm):
class Meta:
model = Customer
exclude = ('User','adder','addtime', 'adderip','status')
2.使用form
form = CustomerForm(request.POST)
if form.is_valid():
form.save()
3.显示
<article>
<span>{{ f.label_tag }} :</span>
{{ f }}
<span class="help">{{ f.help_text }}</span>
<span class="error">{{ f.errors.as_text }}</span>
</article>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询