可以我覆盖Django渲染标签内的复选框输入吗?

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

Can I override Django rendering checkbox inputs inside a label?

问题

我正在尝试渲染一个表单,该表单允许通过复选框进行多个选择。

我尝试过使用django-multiselectfield(我已经在使用的库)和原生的CheckboxSelectMultiple小部件。

如果我有一个简单的表单如下:

FAVORITE_COLORS_CHOICES = [
    ('blue', 'Blue'),
    ('green', 'Green'),
    ('black', 'Black'),
]

class SimpleForm(forms.Form):
    favorite_colors = forms.MultipleChoiceField(
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=FAVORITE_COLORS_CHOICES,
    )

如果我使用小部件调整渲染如下:

{% render_field form.favorite_colors %}

我会得到以下HTML输出:

<div id="id_favorite_colors">
    <div>
        <label for="id_favorite_colors_0">
        <input class="form-control" id="id_favorite_colors_0" name="favorite_colors" placeholder="Your Name" type="checkbox" value="blue" />
        Blue</label>
    </div>
    <div>
        <label for="id_favorite_colors_1">
        <input class="form-control" id="id_favorite_colors_1" name="favorite_colors" placeholder="Your Name" type="checkbox" value="green" />
        Green</label>
    </div>
    <div>
        <label for="id_favorite_colors_2">
        <input class="form-control" id="id_favorite_colors_2" name="favorite_colors" placeholder="Your Name" type="checkbox" value="black" />
        Black</label>
    </div>
</div>

这会对我渲染表单的方式造成问题。是否有办法强制Django输出输入然后标签,使HTML如下所示:

<div id="id_favorite_colors">
    <div>
        <input class="form-control" id="id_favorite_colors_0" name="favorite_colors" placeholder="Your Name" type="checkbox" value="blue" />
        <label for="id_favorite_colors_0">Blue</label>            
    </div>
    <div>
        <input class="form-control" id="id_favorite_colors_1" name="favorite_colors" placeholder="Your Name" type="checkbox" value="green" />
        <label for="id_favorite_colors_1">Green</label>            
    </div>
    <div>
        <input class="form-control" id="id_favorite_colors_2" name="favorite_colors" placeholder="Your Name" type="checkbox" value="black" />
        <label for="id_favorite_colors_2">Black</label>            
    </div>
</div>

我没有找到一个简单的答案,大多数我找到的讨论都是来自大约10年前左右。

英文:

I'm trying to render a form which allows for multiple selections via checkboxes.

I've tried with django-multiselectfield (which I was already using) and the native CheckboxSelectMultiple widget.

If I have a simple form like so:

FAVORITE_COLORS_CHOICES = [
    (&#39;blue&#39;, &#39;Blue&#39;),
    (&#39;green&#39;, &#39;Green&#39;),
    (&#39;black&#39;, &#39;Black&#39;),
]

class SimpleForm(forms.Form):
    favorite_colors = forms.MultipleChoiceField(
        required=False,
        widget=forms.CheckboxSelectMultiple,
        choices=FAVORITE_COLORS_CHOICES,
    )

And if I render with widget tweaks like so:

{% render_field form.favorite_colors %}

I get the following HTML output

&lt;div id=&quot;id_favorite_colors&quot;&gt;
    &lt;div&gt;
        &lt;label for=&quot;id_favorite_colors_0&quot;&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_0&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;blue&quot; /&gt;
        Blue&lt;/label&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;id_favorite_colors_1&quot;&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_1&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;green&quot; /&gt;
        Green&lt;/label&gt;
    &lt;/div&gt;
    &lt;div&gt;
        &lt;label for=&quot;id_favorite_colors_2&quot;&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_2&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;black&quot; /&gt;
        Black&lt;/label&gt;
    &lt;/div&gt;
&lt;/div&gt;

This is causing problems for the way I want to render my form. Is there any way I can force Django to output the input and then the label, so the html would be like so:

&lt;div id=&quot;id_favorite_colors&quot;&gt;
    &lt;div&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_0&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;blue&quot; /&gt;
        &lt;label for=&quot;id_favorite_colors_0&quot;&gt;Blue&lt;/label&gt;            
    &lt;/div&gt;
    &lt;div&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_1&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;green&quot; /&gt;
        &lt;label for=&quot;id_favorite_colors_1&quot;&gt;Green&lt;/label&gt;            
    &lt;/div&gt;
    &lt;div&gt;
        &lt;input class=&quot;form-control&quot; id=&quot;id_favorite_colors_2&quot; name=&quot;favorite_colors&quot; placeholder=&quot;Your Name&quot; type=&quot;checkbox&quot; value=&quot;black&quot; /&gt;
        &lt;label for=&quot;id_favorite_colors_2&quot;&gt;Black&lt;/label&gt;            
    &lt;/div&gt;
&lt;/div&gt;

I haven't found an easy answer to this and most of the discussions I've found are from 10 years ago or so.

答案1

得分: 1

你可以尝试这个,看看它是否适合你。

你的模板会看起来像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="id_favorite_colors">
        {% for choice in form.favorite_colors %}
        <div>
            <label for="id_favorite_colours_{{ forloop.counter0 }}">{{ choice.choice_label }}</label>
            {{ choice.tag }}
        </div>
        {% endfor %}
    </div>
</body>
</html>

而你的views.py将会如下所示:

def simple(request):
    form = SimpleForm()
    # 你可以添加任何你想要用于表单的自定义逻辑
    return render(request, "test.html", {"form": form})
英文:

You can try this out to find if it will work for you.

Your template will look some thing like this

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;id_favorite_colors&quot;&gt;
        {% for choice in form.favorite_colors %}
        &lt;div&gt;
            &lt;label for=&quot;id_favorite_colours_{{ forloop.counter0 }}&quot;&gt;{{choice.choice_label}}&lt;/label&gt;
            {{ choice.tag }}
        &lt;/div&gt;
        {% endfor %}
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

And your views.py will look like this

def simple(request):
    form = SimpleForm()
    # Any custom logic you want to use for the form
    return render(request, &quot;test.html&quot;, {&quot;form&quot;:form})

huangapple
  • 本文由 发表于 2023年3月7日 02:28:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654520.html
匿名

发表评论

匿名网友

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

确定