django动态表单设置max_length属性
我正在为我的应用程序创建一个“表单管理”系统。
我正在使用自定义表单“工厂”方法动态创建表单。
表单数据位于 json 文件中。
我可以创建一个 forms.CharField 并设置 label、required、initial 和 help_text 属性。
当我尝试设置 max_length 属性时,我没有收到任何错误消息,但生成的 HTML 不包含 max_length 属性。
在静态(类)形式中定义为
class SearchAccountForm(forms.Form):
provider_code = forms.CharField(
label='Provider:',
max_length=100,
required=True,
widget=forms.TextInput(attrs={'class': 'form-control'}))
结果 HTML 包含 max_length 属性。
<label for="id_provider_code">Provider:</label>
</th><td><input type="text" name="provider_code" class="form-control" maxlength="100" required id="id_provider_code">
那么 max_length 是怎么回事?
Json 文件
{
"form1": [
{
"fld_name": "customer_name",
"fld_type": "CharField",
"fld_label": "Cust Name",
"fld_required": "False",
"fld_maxLength": 5,
"initial": "Dr John"
},
{
"fld_name": "customer_number",
"fld_type": "CharField",
"fld_label": "Cust #",
"fld_required": "True",
"fld_maxLength": 15,
"help_text": "Enter account number"
},
{
"fld_name": "customer_type",
"fld_type": "CharField",
"fld_label": "Customer Type",
"fld_required": "False"
}
]
}
和 forms.py 工厂方法
from django import forms
import json
def dynfrm():
f = open('blog/frmJson/frm1.json')
data = json.load(f)
fields = {}
for i in data['form1']: ## form1 = form name in json file
print(i)
## add to fields list
if i['fld_type'] == 'CharField':
fields[i["fld_name"]] = forms.CharField()
if 'fld_label' in i:
fields[i["fld_name"]].label = i["fld_label"]
if 'fld_required' in i:
if i["fld_required"] == 'False':
fields[i["fld_name"]].required = False
else:
fields[i["fld_name"]].required = True
if 'initial' in i: fields[i["fld_name"]].initial = i["initial"]
if 'help_text' in i: fields[i["fld_name"]].help_text = i["help_text"]
## next line not working
if 'fld_maxLength' in i: fields[i["fld_name"]].max_length = i["fld_maxLength"]
fields[i["fld_name"]].widget = forms.TextInput()
return type('DynForm', # form name is irrelevant
(forms.BaseForm,),
{'base_fields': fields})
我的视图
def vdynfrm(request):
if request.method == 'POST':
form = dynfrm(request.POST)
if form.is_valid():
pass
## all good
else:
form = dynfrm()
##return render(request, "blog/dfrm.html",{'form': form})
return render(request, "blog/searchAccount.html",{'form': form})
和生成的 HTML
<div class="form-group">
<form action="/searchAccount/" method="post">
<table>
<tr>
<th><label for="id_customer_name">Cust Name:</label></th>
<td><input type="text" name="customer_name" value="Dr John" id="id_customer_name">/td>
</tr>
<tr>
<th><label for="id_customer_number">Cust #:</label></th>
<td><input type="text" name="customer_number" required id="id_customer_number"><br>
<span class="helptext">Enter account number</span></td>
</tr>
<tr>
<th><label for="id_customer_type">Customer Type:</label></th>
<td><input type="text" name="customer_type" id="id_customer_type"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</div>
I am creating a 'Forms Management' system for my application.
I am creating a forms dynamically using a custom form 'factory' method.
Form data is in a json file.
I can create a forms.CharField and set the label, required, initial and help_text properties.
When I try to set the max_length property I do not get any error message, but the resulting HTML does not contain the max_length attribute.
In static (class) forms defined as
class SearchAccountForm(forms.Form):
provider_code = forms.CharField(
label='Provider:',
max_length=100,
required=True,
widget=forms.TextInput(attrs={'class': 'form-control'}))
The resulting HTML contains the max_length attribute.
<label for="id_provider_code">Provider:</label>
</th><td><input type="text" name="provider_code" class="form-control" maxlength="100" required id="id_provider_code">
So what's up with max_length??
Json file
{
"form1": [
{
"fld_name": "customer_name",
"fld_type": "CharField",
"fld_label": "Cust Name",
"fld_required": "False",
"fld_maxLength": 5,
"initial": "Dr John"
},
{
"fld_name": "customer_number",
"fld_type": "CharField",
"fld_label": "Cust #",
"fld_required": "True",
"fld_maxLength": 15,
"help_text": "Enter account number"
},
{
"fld_name": "customer_type",
"fld_type": "CharField",
"fld_label": "Customer Type",
"fld_required": "False"
}
]
}
and the forms.py factory method
from django import forms
import json
def dynfrm():
f = open('blog/frmJson/frm1.json')
data = json.load(f)
fields = {}
for i in data['form1']: ## form1 = form name in json file
print(i)
## add to fields list
if i['fld_type'] == 'CharField':
fields[i["fld_name"]] = forms.CharField()
if 'fld_label' in i:
fields[i["fld_name"]].label = i["fld_label"]
if 'fld_required' in i:
if i["fld_required"] == 'False':
fields[i["fld_name"]].required = False
else:
fields[i["fld_name"]].required = True
if 'initial' in i: fields[i["fld_name"]].initial = i["initial"]
if 'help_text' in i: fields[i["fld_name"]].help_text = i["help_text"]
## next line not working
if 'fld_maxLength' in i: fields[i["fld_name"]].max_length = i["fld_maxLength"]
fields[i["fld_name"]].widget = forms.TextInput()
return type('DynForm', # form name is irrelevant
(forms.BaseForm,),
{'base_fields': fields})
my view
def vdynfrm(request):
if request.method == 'POST':
form = dynfrm(request.POST)
if form.is_valid():
pass
## all good
else:
form = dynfrm()
##return render(request, "blog/dfrm.html",{'form': form})
return render(request, "blog/searchAccount.html",{'form': form})
and the resulting HTML
<div class="form-group">
<form action="/searchAccount/" method="post">
<table>
<tr>
<th><label for="id_customer_name">Cust Name:</label></th>
<td><input type="text" name="customer_name" value="Dr John" id="id_customer_name">/td>
</tr>
<tr>
<th><label for="id_customer_number">Cust #:</label></th>
<td><input type="text" name="customer_number" required id="id_customer_number"><br>
<span class="helptext">Enter account number</span></td>
</tr>
<tr>
<th><label for="id_customer_type">Customer Type:</label></th>
<td><input type="text" name="customer_type" id="id_customer_type"></td>
</tr>
</table>
<input type="submit" value="Submit">
</form>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当您将
上下文
正确发送到模板
文件时,max_length
属性才起作用。您的 forms.py
使用基于
function
的视图:如果您忘记为
get
请求方法提供else
条件,那么您将不会收到django 的
内置错误消息以及max_length
等。使用基于类的视图,可以轻松处理它:
从上面的两个示例来看,
max_length
属性工作正常,因为它的get
请求已得到处理。检查你的views.py,它可能会有所帮助。
The
max_length
property only works when you send thecontext
correctly to yourtemplate
file.your forms.py
With
function
based view:If you forget to give
else
condition forget
request method, so you will not receivedjango's
inbuild error messages as well asmax_length
etc.With Class based view it can be handled easily:
From both the examples above
max_length
property is working properly because itsget
request got handled.Check your
views.py
, it may help.