How to show Radio Button type fields in the form using Django?

Asked

Viewed 1,451 times

0

I created a file called const.py (where I have the options of Choices that I will call in the models) as follows::

const.

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

py.models

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 html form.

<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 what I call these options in html! For example, I want you to appear with the radiobutton type in my form.html

2 answers

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']

py views.

# -*- 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 admin too.

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)

0


You will have to create a Form for your Model, and in it indicate that the field should have the widget Radioselect:

In Forms.py

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

Na views.py:

form = ForensicTrafficForm()

In the 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: https://docs.djangoproject.com/en/1.8/ref/forms/widgets/#radioselect

  • 1

    Hello guys, thanks for the help!! Have a nice day :)!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.