Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render the radio buttons without ul in django

forms.py

Date_Format = (
    ('0', ' dd / mm / yyyy'),
    ('1', 'mm / dd / yyyy'),
)    
Time_Format = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)
class SettingsForm(forms.ModelForm):
    date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format)
    time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format)

template.py

Update:

                    {% for radio in SettingsForm.date_format %}
                    {{ radio.choice_label }}
                    <div class="select">{{ radio.tag }}</div>
                    {% endfor %}

The above form is rendering the radio buttons in a unordered list, but I want to be with out unordered list. How can I perform this?

like image 393
user2086641 Avatar asked May 13 '13 12:05

user2086641


1 Answers

You could create a custom renderer:

from django import forms
from django.forms.widgets import RadioFieldRenderer
from django.shortcuts import render
from django.utils.html import format_html_join
from django.utils.encoding import force_text


class RadioFieldWithoutULRenderer(RadioFieldRenderer):

    def render(self):
        return format_html_join(
            '\n',
            '<li>{0}</li>',
            [(force_text(w), ) for w in self],
        )


CHOICES = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)


class MyForm(forms.Form):
    somefield = forms.ChoiceField(
        widget=forms.RadioSelect(renderer=RadioFieldWithoutULRenderer),
        choices=CHOICES)


def myview(request):
    return render(request, 'form.html', {'form': MyForm()})
like image 175
Jeroen Vloothuis Avatar answered Sep 30 '22 17:09

Jeroen Vloothuis