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.
如何在django实施这种表格(请注意,用户不能选择不止一个答案)
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.
如何在django实施这种表格(请注意,用户不能选择不止一个答案)
http://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield” 无线电台:
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(),
}
2. 您认为,在形式上,具体指明使用:
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.
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>
<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>
<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>
<li><label for="id_first_name">First name:</label> <input id="id_first_name" type="text" name="first_name" maxlength="32" /></li>
<li><label for="id_last_name">Last name:</label> <input id="id_last_name" type="text" name="last_name" maxlength="32" /></li>
<li><label for="id_company">Company:</label> <input id="id_company" type="text" name="company" maxlength="32" /></li>
<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()
.
Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...
I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...
Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...
Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...
I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...
Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...
Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...
I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...