如何防止Django字段被包裹在`div`中?

huangapple go评论77阅读模式
英文:

How to prevent django fields from being wrapped in a `div`?

问题

以下是翻译好的部分:

<label class="user-item">
    <input name="user_input" type="radio" ... >
    <span>Lorem ipsum dolor</span>
</label>
<label class="user-item">
    <input name="user_input" type="radio" ... >
    <span>Lorem ipsum dolor</span>
</label>

希望这能帮助您达到预期的结果。

英文:

If I use the below to generate multiple radio buttons:

from django import forms


class MCQCollectionForm(forms.Form):
    user_input = forms.ChoiceField(
        widget=forms.RadioSelect, label='', choices=enumerate(['option 1', 'option 2'])
    )

I get:

<div id="id_user_input">
    <div>
        <label for="id_user_input_0"><input id="id_user_input_0" name="user_input" required="" type="radio"
                                            value="0">
            option 1</label>

    </div>
    <div>
        <label for="id_user_input_1"><input id="id_user_input_1" name="user_input" required="" type="radio"
                                            value="1">
            option 2</label>

    </div>
</div>

but I want it to be only the below, nothing else:

<label class="user-item">
    <input name="user_input" type="radio" ... >
    <span>Lorem ipsum dolor</span>
</label>
<label class="user-item">
    <input name="user_input" type="radio" ... >
    <span>Lorem ipsum dolor</span>
</label>

is there a simple way to do so without having to use JS and manually make the desired changes? Note: the extra attributes id=..., required, ... are fine, I just want the divs gone and be able to set label's class to user-item and span with its contents.

答案1

得分: 1

以下是您要翻译的部分:

  1. 在您的forms.py文件中添加此自定义小部件类。

    class CustomRadioSelect(forms.RadioSelect):
        def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
            option = super().create_option(name, value, label, selected, index, subindex, attrs)
            option["wrapper_attrs"] = {}
            if "label_attrs" not in option:
                option["label_attrs"] = {}
            option["label_attrs"]["class"] = "user-item"
            return option
    
  2. 现在,使用以下代码更新您的MCQCollectionForm类:

    class MCQCollectionForm(forms.Form):
        user_input = forms.ChoiceField(
            widget=CustomRadioSelect, label='', choices=enumerate(['option 1', 'option 2'])
        )
    
  3. 从您的views.py文件中通过适当的视图上下文传递表单,如下所示: form = MCQCollectionForm() render(.....,{'form':form})
    (我相信您已经将此放在适当位置)

  4. 从您的模板文件中按照以下方式循环遍历表单字段:

    {% for choice in form1.user_input %}
        <label class="{{ choice.label.attrs.class }}" for="{{ choice.id_for_label }}">
            {{ choice.tag }}
            <span>{{ choice.choice_label }}</span>
        </label>
    {% endfor %}
    

这将帮助您实现您的目标。下面是它在HTML源代码中的格式示例的屏幕截图。

如何防止Django字段被包裹在`div`中?

英文:

You can do it by using a custom widget inherited from forms.RadioSelect and overriding the method create_option

In your forms.py file add this custom widget class.

class CustomRadioSelect(forms.RadioSelect):
    def create_option(self, name, value, label, selected, index, subindex=None, attrs=None):
        option = super().create_option(name, value, label, selected, index, subindex, attrs)
        option[&quot;wrapper_attrs&quot;] = {}
        if &quot;label_attrs&quot; not in option:
            option[&quot;label_attrs&quot;] = {}
        option[&quot;label_attrs&quot;][&quot;class&quot;] = &quot;user-item&quot;
        return option

Now, Update your MCQCollectionForm class with this:

class MCQCollectionForm(forms.Form):
    user_input = forms.ChoiceField(
        widget=CustomRadioSelect, label=&#39;&#39;, choices=enumerate([&#39;option 1&#39;, &#39;option 2&#39;])
    )

From your views.py file pass the form through your appropriate view context like: form = MCQCollectionForm() render(.....,{&#39;form&#39;:form})
(I believe you already have this in place)

From your template file loop through the form fields like below:

{% for choice in form1.user_input %}
    &lt;label class=&quot;{{ choice.label.attrs.class }}&quot; for=&quot;{{ choice.id_for_label }}&quot;&gt;
    {{ choice.tag }}
       &lt;span&gt;{{ choice.choice_label }}&lt;/span&gt;
     &lt;/label&gt;
{% endfor %}

This will help you to achieve your goal. Below is a screenshot of how it will format in HTML source code.

如何防止Django字段被包裹在`div`中?

huangapple
  • 本文由 发表于 2023年3月23日 08:11:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定