Django 1.11 Horizontal Selection Field

I just upgraded from Django 1.10 to 1.11.1. In my template new_house_edit.html, I have the following:

{{ form.rating }}

models.py contain the following:

class NewHouse(models.Model):
    rating = models.IntegerField(choices=(
                                    (1, "1"),
                                    (2, "2"),
                                    (3, "3"),
                                    (4, "4"),
                                    (5, "5"),
                                    ),
                                    default=3
                            )

As forms.pyI did the following:

class HorizontalRadioRenderer(forms.RadioSelect.renderer):
    def render(self):
        return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))

class NewHouseForm(forms.ModelForm):

    class Meta:
        model = NewHouse
        fields = (
                'rating',)
        widgets={
                "rating": forms.RadioSelect(renderer=HorizontalRadioRenderer),
                }

Which gave the following error AttributeError: type object 'RadioSelect' has no attribute 'renderer'.I tried to solve it by doing this without working:

class HorizontalRadioSelect(forms.RadioSelect):
    template_name = 'new_house_edit'


class NewHouseForm(forms.ModelForm):

    class Meta:
        model = NewHouse
        fields = (
                'rating',)
        widgets={
                "rating": "rating": forms.ChoiceField(widget=HorizontalRadioSelect, choices=(1, 2, 3, 4, 5)),
                }

Now I get the error AttributeError: 'ChoiceField' object has no attribute 'use_required_attribute'. Can someone help me fix this?

+4
source share
3 answers

In the second code snippet, you pass the form field object to dict widgets instead of the widget object. The correct code looks like this:

class HorizontalRadioSelect(forms.RadioSelect):
    template_name = 'horizontal_select.html'


class NewHouseForm(forms.ModelForm):

    class Meta:
        model = NewHouse
        fields = (
                'rating',)
        widgets={
                "rating": HorizontalRadioSelect()
                }

In your application directory, create a templates folder and add horizontal_select.html with the following html.

{% with id=widget.attrs.id %}
    <ul{% if id %} id="{{ id }}"{% endif %}{% if widget.attrs.class %} class="{{ widget.attrs.class }}"{% endif %}>
        {% for group, options, index in widget.optgroups %}
            {% if group %}
                <li>{{ group }}
                <ul{% if id %} id="{{ id }}_{{ index }}"{% endif %}>
            {% endif %}
            {% for option in options %}
                <li style="display: inline-block">{% include option.template_name with widget=option %}</li>
            {% endfor %}
            {% if group %}
                </ul>
                </li>
            {% endif %}
        {% endfor %}
    </ul>
{% endwith %}
+6
source

-, ChoiceField - .

, ,

class NewHouseForm(forms.ModelForm):
    CHOICES = ((1, "1"),
               (2, "2"),
               (3, "3"),
               (4, "4"),
               (5, "5"))

    rating = forms.ChoiceField(choices=CHOICES,
       widget=forms.RadioSelect(attrs={'class': 'radio-inline'}),
       )

    class Meta:
        model = NewHouse
        fields = (
            'rating',)

-select template_name , .
"" app_level "" , , .

class HorizontalRadioSelect(forms.RadioSelect):
    template_name = 'new_house_edit.html'

"" .

Django "" .

+2

, , , ( 1,8 1.11). .

( "radio_list" - ):

{% for radio in radio_list %}
    {{ radio }}
{% endfor %} 

. , ? , . RadioSelect.

The moral of this little tale? Read the docs. Django is probably the most documented application I've ever worked with. If I did this earlier, it would save me a lot of time today when I could do something more interesting.

Hope this helps someone.

+1
source

Source: https://habr.com/ru/post/1677931/


All Articles