Reputation: 3732
hello .
im new to django and i want to create a form that looks like the image above in html. the form should save the data when the user chose a radio button.
how to implement such a form in django ( please note that user cannot chose more than one answer)
Upvotes: 2
Views: 5337
Reputation: 12638
There's a good example how to do this here: https://code.djangoproject.com/wiki/CookBookNewFormsDynamicFields
Basically, your form code will follow this pattern:
from django import forms
class MyForm(forms.Form):
static_field_a = forms.CharField(max_length=32)
static_field_b = forms.CharField(max_length=32)
static_field_c = forms.CharField(max_length=32)
def __init__(self, dynamic_field_names, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
for field_name in dynamic_field_names:
self.fields[field_name] = forms.CharField(max_legth=32) # creates a dynamic field
Example usage:
>>> dynamic_fields = ('first_name', 'last_name', 'company', 'title')
>>> my_form = MyForm(dynamic_field_names=dynamic_fields)
>>> my_form.as_ul()
u'<li><label for="id_static_field_a">Static field a:</label> <input id="id_static_field_a" type="text" name="static_field_a" maxlength="32" /></li>\n<li><label for="id_static_field_b">Static field b:</label> <input id="id_static_field_b" type="text" name="static_field_b" maxlength="32" /></li>\n<li><label for="id_static_field_c">Static field c:</label> <input id="id_static_field_c" type="text" name="static_field_c" maxlength="32" /></li>\n<li><label for="id_first_name">First name:</label> <input id="id_first_name" type="text" name="first_name" maxlength="32" /></li>\n<li><label for="id_last_name">Last name:</label> <input id="id_last_name" type="text" name="last_name" maxlength="32" /></li>\n<li><label for="id_company">Company:</label> <input id="id_company" type="text" name="company" maxlength="32" /></li>\n<li><label for="id_title">Title:</label> <input id="id_title" type="text" name="title" maxlength="32" /></li>
That'll render a form with the following text inputs:
Just use forms.ChoiceField()
, or whatever you want, instead of forms.TextField()
.
Upvotes: 5
Reputation: 53879
You want to use ChoiceField and RadioSelect:
from django import forms
class GenderForm(forms.Form):
CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
choice = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())
Remember, Django documentation is your friend!
Dynamically Changing Choices If you want to be able to dynamically create the form, it might be a good idea to use ModelForm instead of forms.Form. Here's an example:
from django.db import models
from django.forms import ModelForm
from django import forms
class Answer(models.Model):
answer = models.CharField(max_length=100)
def __unicode__(self):
return self.answer
class Question(models.Model):
title = models.CharField(max_length=100)
answers = models.ManyToManyField('Answer')
class QuestionForm(ModelForm):
class Meta:
model = Question
fields = ('title', 'answers')
widgets = {
'answers': forms.RadioSelect(),
}
In your view instantiate the form with an instance specified to use:
question = Question.objects.order_by('?')[0]
form = QuestionForm(instance=question)
The form will then use the answers associated with that Question (in this case randomly chosen) and pass the form to the templates context as usual.
Upvotes: 7