How to display Radio Button type fields in form using Django?

0

I created a file called const.py (where I have the choices I'll call in models) as follows:

const.py

FORENSIC_TRAFFIC_LIGHTING = (
    (u'1', u'Boa'),
    (u'2', u'Ruim'),
    (u'3', u'Ausente'),
)

models.py

class ForensicTraffic(models.Model):
    """
    Classe para modelagem 
    """
    lighting = models.CharField( 
    choices=const.FORENSIC_TRAFFIC_LIGHTING,
    max_length=1, 
    verbose_name='Iluminação'
)

Here's a piece of form.html

<div class="row">
    <div class="col-xs-4">
        <label>{{traffic_form.lighting.label }}</label>
        <div class="radio">
            <input type="radio" name="optionsRadiosLighting" id="optionsRadiosBoa" value="option1" checked>
            <label for="optionsRadiosBoa">Boa</label>
        </div>
        <div class="radio">
            <input type="radio" name="optionsRadiosLighting" id="optionsRadiosRuim" value="option2">
            <label for="optionsRadiosRuim">Ruim</label>
        </div>
        <div class="radio">
            <input type="radio" name="optionsRadiosLighting" id="optionsRadiosAusente" value="option3">
            <label for="optionsRadiosAusente">Ausente</label>
        </div>
    </div>
</div>

I would like to know how I call these options in html! For example, I want it to appear with the radioButton type in my form.html

    
asked by anonymous 18.10.2015 / 20:23

2 answers

0

You will have to create a Form for your Model, and it indicates that the field should have the RadioSelect widget:

No forms.py

class ForensicTrafficForm(forms.ModelForm):
    lighting = forms.CharField(widget=forms.RadioSelect(choices=FORENSIC_TRAFFIC_LIGHTING))
    class Meta:
        model = ForensicTraffic
        fields = ['lighting']

In views.py:

form = ForensicTrafficForm()

No template:

{% for radio in form.lighting %}
    <label for="{{ radio.id_for_label }}">
        {{ radio.choice_label }}
        <span class="radio">{{ radio.tag }}</span>
    </label>
{% endfor %}

More information in the Django documentation: link

    
18.10.2015 / 21:08
1

forms.py

# -*- coding: utf-8 -*-
from django import forms
from models import ForensicTraffic
import const


class ForensicTrafficForm(forms.ModelForm):
    lighting = forms.CharField(widget=forms.RadioSelect(
        choices=const.FORENSIC_TRAFFIC_LIGHTING))
    class Meta:
        model = ForensicTraffic
        fields = ['lighting']

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
from forms import ForensicTrafficForm


def forensic_traffic(request):
    form = ForensicTrafficForm()
    return render_to_response("forensic_traffic.html", locals(),
                                context_instance=RequestContext( request ))

If you want to use radiobuttons in the admin as well.

admin.py

from django.contrib import admin
from models import ForensicTraffic
from forms import ForensicTrafficForm


class ForensicTrafficAdmin(admin.ModelAdmin):
    form = ForensicTrafficForm

admin.site.register(ForensicTraffic, ForensicTrafficAdmin)
    
20.10.2015 / 22:41