django表单字段显示不顺序
我有一个组的此表格和模型:
class GroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ('leader', 'description', 'size', 'max_size', 'motto')
widgets = {
'size': forms.CheckboxInput(attrs={'id': 'size'}),
'max_size': forms.TextInput(attrs={'type': 'hidden', 'id': 'maxSize'}),
}
该组的创建者可以选择size
的选择,然后我使用javascript更改max_size <代码>显示
。
在我的create_group.html
模板中:
<script>
let size = document.getElementById('size')
let maxSize = document.getElementById('maxSize')
let checked = false
size.onclick = () => {
checked = !checked
if (checked === true) {
maxSize.type = 'show'
} else {
maxSize.type = 'hidden'
}
}
</script>
现在,这效果很好,唯一的问题是字段显示不顺序。
当页面加载时,max_size
是false
,并且未显示其字段。这很好。但是,当用户检查该组的大小时,随后,max_size
显示show> show
的显示时,该字段在座右铭字段之后出现,而不在其根据fields =('Leader','描述','size','max_size','Motto')
正确订购。
此外,max_size
字段包含座右铭
元素本身,而不是其自己的字段:
与其他字段中的其他字段,这些字段都是自己的&lt; p&gt;&lt;/p&gt;
。
I have this form and model for a group:
class GroupForm(forms.ModelForm):
class Meta:
model = Group
fields = ('leader', 'description', 'size', 'max_size', 'motto')
widgets = {
'size': forms.CheckboxInput(attrs={'id': 'size'}),
'max_size': forms.TextInput(attrs={'type': 'hidden', 'id': 'maxSize'}),
}
The creator of the group has an option to check yes for size
and on doing so, I used javascript to change the type of max_size
to show
.
In my create_group.html
template:
<script>
let size = document.getElementById('size')
let maxSize = document.getElementById('maxSize')
let checked = false
size.onclick = () => {
checked = !checked
if (checked === true) {
maxSize.type = 'show'
} else {
maxSize.type = 'hidden'
}
}
</script>
Now, this works fine, the only problem is that the fields are displayed out of order.
When the page loads, max_size
is false
and its field is not displayed. Which is good. However, when the user checks that group has a size, and, subsequently, the max_size
has a display of show
, the field shows up after the motto field and not in its correct order according to fields = ('leader', 'description', 'size', 'max_size', 'motto')
.
Furthermore, the max_size
field is included inside the motto
element itself and not as its own field:
vs. the other fields which are all in their own <p></p>
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜想
{{form.as_p}}
等。首先渲染所有可见字段,然后呈现隐藏的字段。您可以按照模板中想要的顺序明确渲染字段。与其在模板中进行硬编码,还可以(我从未尝试过):
将模板以您想要的顺序明确地传递给字段列表:
在模板中
,您可以使该字段的隐藏在此字段中已完成。由JS
I'm guessing that
{{form.as_p}}
etc. render all the visible fields first, then the hidden ones.You can explicitly render the fields in the order you want in your template. Rather than hardcoding the order in your template, maybe this (I've never tried this):
Pass to your template a list of fields explicitly in the order you want:
In the template
Or, you can make the hiding of this field something done by JS
如果其他人遇到了这个问题,也可以只使用JS和CSS。现在,我正在使用JavaScript查看
size
是否已选中,如果是maxsize.style.display ='block'
vsmaxsize.style。 display ='none'
如果size
未检查。然后,我仍然可以看到Django的表格标签问题。为了解决这个问题,我在您可以自己看到。
我现在的问题是,我不知道如何添加仅在可见表单字段时可见的标签。
If anyone else comes across this issue, it's also possible to just use js and css. Right now, I'm using javascript to see if
size
is checked and if it is thenmaxSize.style.display = 'block'
vsmaxSize.style.display = 'none'
ifsize
isn't checked.Then I had the issue of django's form label still being visible. To fix that I saw an answer on dev.to which you can see for yourself.
My issue now is that I don't know how to add a label that is only visible when the form field is visible.