django表单字段显示不顺序

发布于 2025-02-13 08:44:26 字数 1475 浏览 2 评论 0原文

我有一个组的此表格和模型:

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_sizefalse,并且未显示其字段。这很好。但是,当用户检查该组的大小时,随后,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:

enter image description here

vs. the other fields which are all in their own <p></p>.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2025-02-20 08:44:26

我猜想{{form.as_p}}等。首先渲染所有可见字段,然后呈现隐藏的字段。

您可以按照模板中想要的顺序明确渲染字段。与其在模板中进行硬编码,还可以(我从未尝试过):

FIELD_ORDER = ( ('leader', 'description', 'size', 'max_size', 'motto')
class GroupForm(forms.ModelForm):
    class Meta:
        model = Group
        fields = FIELD_ORDER

将模板以您想要的顺序明确地传递给字段列表:

fields_in_order = [ form[x] for x in FIELD_ORDER ]

在模板中

{% for field in fields_in_order %}
   {{field}}
{% endfor %}

,您可以使该字段的隐藏在此字段中已完成。由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):

FIELD_ORDER = ( ('leader', 'description', 'size', 'max_size', 'motto')
class GroupForm(forms.ModelForm):
    class Meta:
        model = Group
        fields = FIELD_ORDER

Pass to your template a list of fields explicitly in the order you want:

fields_in_order = [ form[x] for x in FIELD_ORDER ]

In the template

{% for field in fields_in_order %}
   {{field}}
{% endfor %}

Or, you can make the hiding of this field something done by JS

待天淡蓝洁白时 2025-02-20 08:44:26

如果其他人遇到了这个问题,也可以只使用JS和CSS。现在,我正在使用JavaScript查看size是否已选中,如果是maxsize.style.display ='block' vs maxsize.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 then maxSize.style.display = 'block' vs maxSize.style.display = 'none' if size 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文