英文:
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
以下是您要翻译的部分:
-
在您的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
-
现在,使用以下代码更新您的MCQCollectionForm类:
class MCQCollectionForm(forms.Form): user_input = forms.ChoiceField( widget=CustomRadioSelect, label='', choices=enumerate(['option 1', 'option 2']) )
-
从您的
views.py
文件中通过适当的视图上下文传递表单,如下所示:form = MCQCollectionForm() render(.....,{'form':form})
(我相信您已经将此放在适当位置) -
从您的模板文件中按照以下方式循环遍历表单字段:
{% 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源代码中的格式示例的屏幕截图。
英文:
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["wrapper_attrs"] = {}
if "label_attrs" not in option:
option["label_attrs"] = {}
option["label_attrs"]["class"] = "user-item"
return option
Now, Update your MCQCollectionForm
class with this:
class MCQCollectionForm(forms.Form):
user_input = forms.ChoiceField(
widget=CustomRadioSelect, label='', choices=enumerate(['option 1', 'option 2'])
)
From your views.py
file pass the form through your appropriate view context like: form = MCQCollectionForm() render(.....,{'form':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 %}
<label class="{{ choice.label.attrs.class }}" for="{{ choice.id_for_label }}">
{{ choice.tag }}
<span>{{ choice.choice_label }}</span>
</label>
{% endfor %}
This will help you to achieve your goal. Below is a screenshot of how it will format in HTML source code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论