[PYTHON] [Django] Install radio buttons and shape markup

environment

Django 1.10.5


Make form.py

First, form.py, which is the source of form There is also a way to use ModelForm, but this time we will create something that is completed in form.py

form.py


from django import forms
from django.contrib.admin import widgets
import os

CHOICE = {
    ('0','cute'),
    ('1','cool'),
    ('2','passion'),
}

form SampleForm(forms.Form):
	select = forms.ChoiceField(label='attribute', widget=forms.RadioSelect, choices= CHOICE, initial=0)

You can specify the items selected by default by setting ʻinitial = index Ifrequired = False` does not require selection, it is better to remove it.

Make views.py

This time I will simply write a view that passes only this form

views.py


from django.shortcuts import render, get_object_or_404, redirect
from forms.forms import *

def sample_view(request):
	form = SampleForm
	return render(request,
		project/sample.html,
		{"form" : form}
	)

Call form on html side

As it is Let's do it

sample.html


{{ form }}

Basically, this is all you need, but if there are multiple input items in one form, you can control them individually as follows.

sample.html


{{ form.select.label }}{{ form.select}}

You can easily install a radio button above, but the drawback is that the generated markup is surrounded by `