如何正确使用 Django Forms
2个回答
2016-01-15 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
首先要创建自己的Form类,继承于django的Form,然后创建form中的字段,例子如下:
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."),
}
展开全部
不用Django Forms,自写HTML等。
使用样例:get_http_params(request,"param1","param2")
代码:
def get_http_param(request, param_name, default_val = None):
if http_is_get(request):
if request.GET.has_key(param_name):
return request.GET[param_name]
else:
if request.POST.has_key(param_name):
return request.POST[param_name]
if default_val == None:
raise ArgError("param '%s' absent" % param_name)
return default_val
def get_http_params(request, *param_names):
return [ get_http_param(request, k) for k in param_names ]
使用样例:get_http_params(request,"param1","param2")
代码:
def get_http_param(request, param_name, default_val = None):
if http_is_get(request):
if request.GET.has_key(param_name):
return request.GET[param_name]
else:
if request.POST.has_key(param_name):
return request.POST[param_name]
if default_val == None:
raise ArgError("param '%s' absent" % param_name)
return default_val
def get_http_params(request, *param_names):
return [ get_http_param(request, k) for k in param_names ]
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询