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

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

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

问题

以下是翻译好的部分:

  1. <label class="user-item">
  2. <input name="user_input" type="radio" ... >
  3. <span>Lorem ipsum dolor</span>
  4. </label>
  5. <label class="user-item">
  6. <input name="user_input" type="radio" ... >
  7. <span>Lorem ipsum dolor</span>
  8. </label>

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

英文:

If I use the below to generate multiple radio buttons:

  1. from django import forms
  2. class MCQCollectionForm(forms.Form):
  3. user_input = forms.ChoiceField(
  4. widget=forms.RadioSelect, label='', choices=enumerate(['option 1', 'option 2'])
  5. )

I get:

  1. <div id="id_user_input">
  2. <div>
  3. <label for="id_user_input_0"><input id="id_user_input_0" name="user_input" required="" type="radio"
  4. value="0">
  5. option 1</label>
  6. </div>
  7. <div>
  8. <label for="id_user_input_1"><input id="id_user_input_1" name="user_input" required="" type="radio"
  9. value="1">
  10. option 2</label>
  11. </div>
  12. </div>

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

  1. <label class="user-item">
  2. <input name="user_input" type="radio" ... >
  3. <span>Lorem ipsum dolor</span>
  4. </label>
  5. <label class="user-item">
  6. <input name="user_input" type="radio" ... >
  7. <span>Lorem ipsum dolor</span>
  8. </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文件中添加此自定义小部件类。

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

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

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

    1. {% for choice in form1.user_input %}
    2. <label class="{{ choice.label.attrs.class }}" for="{{ choice.id_for_label }}">
    3. {{ choice.tag }}
    4. <span>{{ choice.choice_label }}</span>
    5. </label>
    6. {% 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.

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

Now, Update your MCQCollectionForm class with this:

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

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:

  1. {% for choice in form1.user_input %}
  2. &lt;label class=&quot;{{ choice.label.attrs.class }}&quot; for=&quot;{{ choice.id_for_label }}&quot;&gt;
  3. {{ choice.tag }}
  4. &lt;span&gt;{{ choice.choice_label }}&lt;/span&gt;
  5. &lt;/label&gt;
  6. {% 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:

确定