简体   繁体   English

django - 更改表单的结构,使输入不在标签中

[英]django - change the structure of forms so that inputs be out of labels

How can we change the structure of forms so that inputs be out of labels when my form render will be displayed like this:我们如何更改表单的结构,以便在我的表单渲染显示如下时输入不在标签中:

<p>
<label for="id_form-0-food_name_0"><input checked="checked" id="id_form-0-food_name_0" name="form-0-food_name" value="" type="radio"> (Nothing)</label>
<label for="id_form-0-food_name_1"><input id="id_form-0-food_name_1" name="form-0-food_name" value="1" type="radio"> خوراک مرغ</label>
<label for="id_form-0-food_name_2"><input id="id_form-0-food_name_2" name="form-0-food_name" value="2" type="radio"> خوراک لوبیا</label>
<label for="id_form-0-food_name_3"><input id="id_form-0-food_name_3" name="form-0-food_name" value="3" type="radio"> فسنجون</label>
</p>

but i need input s render out of the label s tag.但我需要input s 渲染出label s 标签。 like this:像这样:

<p>
<input checked="checked" id="id_form-0-food_name_0" name="form-0-food_name" value="" type="radio"><label for="id_form-0-food_name_0"> (Nothing)</label>
<input id="id_form-0-food_name_1" name="form-0-food_name" value="1" type="radio"><label for="id_form-0-food_name_1"> خوراک مرغ</label>
<input id="id_form-0-food_name_2" name="form-0-food_name" value="2" type="radio"><label for="id_form-0-food_name_2"> خوراک لوبیا</label>
<input id="id_form-0-food_name_3" name="form-0-food_name" value="3" type="radio"><label for="id_form-0-food_name_3"> فسنجون</label>
</p>

my forms.py:我的forms.py:

class Reserve(ModelForm):
    food_name = forms.ModelChoiceField(
        queryset=Food.objects.all(), 
        widget=forms.RadioSelect(renderer=RadioFieldWithoutULRenderer), 
        empty_label="(Nothing)",
        # label=''
        )
    class Meta:
        model = Reservation
        fields = ('food_name',)

and form.htmlform.html

<form method="post">
{% csrf_token %}

{% for form in formset %}

<p>
  <input name="group1" type="radio" id="test1" value="" />
  {{ form.food_name  }}
</p>

{% endfor %}

<button type="submit" class="btn btn-default">Submit</button>

As shown in this question you can create your own widget using a custom template:如本问题所示,您可以使用自定义模板创建自己的小部件:

  1. Create a template file in your templates folder (eg my_project/my_app/templates/my_app/widgets/radio_option.html ):在您的模板文件夹中创建一个模板文件(例如my_project/my_app/templates/my_app/widgets/radio_option.html ):
{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %}<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{{ widget.label }}</label>{% endif %}

(This template is copied and slightly modified from django/forms/templates/django/forms/widgets/input_option.html which is used for radio widgets by default.) (此模板是从django/forms/templates/django/forms/widgets/input_option.html复制并稍微修改的,默认情况下用于无线电小部件。)

  1. Create a custom widget in your app:在您的应用中创建自定义小部件:
class MyRadioSelect(django.forms.RadioSelect):
    option_template_name = "my_app/widgets/radio_option.html"
  1. Use MyRadioSelect instead of RadioSelect使用MyRadioSelect而不是RadioSelect

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM