设置“名称”; Django 表单字段的属性
我正在努力使用 django 设置表单选择字段的“名称”属性。
这就是我正在尝试的:
def make_layers_form(dxf_model):
layers = Layer.objects.filter(dxf_file=dxf_model)
choices = [(m[0], m[1]) for m in settings.MACHINING_CHOICES]
fields = {}
for l in layers:
if l.name.decode() == "0":
# Si le layer 0 existe on l'associe a la "Coupe"
init = settings.MACHINING_CHOICES["CUT"]
else:
# Par défaut on met la valeur "Aucun"
init = settings.MACHINING_CHOICES["NONE"]
fields[l.name] = forms.ChoiceField(choices=choices, initial=init,
widget=forms.Select(attrs={'name':dxf_model.filename+"_"+str(l.pk)}))
return type('LayersForm', (forms.BaseForm,), {'base_fields':fields})
当我观察“名称”属性时,它没有按预期设置...
此外,我尝试在某些情况下设置默认值,但它没有效果。有什么想法吗?
感谢您的帮助!
更新
这是一个html示例
<select name="0" id="3_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
...
<select name="0" id="4_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
每个选择标签都允许将一些数据绑定到之前上传的2个不同文件
如果我没有记错的话,属性“name”的值来自label_tag的值。
而label_tag的值是分别从每个文件中提取的数据。
不幸的是,两个不同的文件可能包含相同的值,该值是为了设置属性“名称”而提取的。这是我的问题!
因此,我将定义每个选择标签的属性名称,如下所示: (文件名)+“_”+(从文件中提取的值)
我不知道我的解释是否清楚......
你知道为什么我正在尝试的方法不起作用吗?
此外,当我设置初始值时,它没有任何效果......
I'm struggling for set the 'name' attribute of a form select field with django.
This is what i'm trying :
def make_layers_form(dxf_model):
layers = Layer.objects.filter(dxf_file=dxf_model)
choices = [(m[0], m[1]) for m in settings.MACHINING_CHOICES]
fields = {}
for l in layers:
if l.name.decode() == "0":
# Si le layer 0 existe on l'associe a la "Coupe"
init = settings.MACHINING_CHOICES["CUT"]
else:
# Par défaut on met la valeur "Aucun"
init = settings.MACHINING_CHOICES["NONE"]
fields[l.name] = forms.ChoiceField(choices=choices, initial=init,
widget=forms.Select(attrs={'name':dxf_model.filename+"_"+str(l.pk)}))
return type('LayersForm', (forms.BaseForm,), {'base_fields':fields})
When I watch to the 'name' attribute, it is not set as expected...
Furthermore I try to set a default value in some case, but it has no effect. Any ideas ?
Thanks for your help!
UPDATE
Here is an html example
<select name="0" id="3_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
...
<select name="0" id="4_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
Each select tag permits to bind some datas to 2 differents files previously uploaded
The value of the attribute 'name' comes from the value of label_tag if I am not wrong.
And the value of the label_tag is a data extracted from each file respectively.
Unfortunately two differents files can contain the same value, which is extracted for setting the attribute 'name'. This is my problem!
So I would define the attribute name of each select tag as it follows:
(filename)+"_"+(the value extracted from the file)
I don't know if my explanations are clear...
Do you know why what i'm trying is not working ?
Further more when I set the initial value, it has no effects...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论