Alright, I\'m at a loss with the Django Forms, as the documentation just doesn\'t seem to quite cover what I\'m looking for. At least it seems to come to a screeching halt once
First, you create a ModelForm for a given Model. In this example I'm doing it for Quiz but you can rinse and repeat for your other models. For giggles, I'm making the "label" be a Select box with preset choices:
from django.models import BaseModel
from django import forms
from django.forms import ModelForm
CHOICES_LABEL = (
('label1', 'Label One'),
('label2', 'Label Two')
)
class Quiz(models.Model):
label = models.CharField(blank=True, max_length=400)
slug = models.SlugField()
def __unicode__(self):
return self.label
class QuizForm(ModelForm):
# Change the 'label' widget to a select box.
label = forms.CharField(widget=forms.Select(choices=CHOICES_LABEL))
class Meta:
# This tells django to get attributes from the Quiz model
model=Quiz
Next, in your views.py you might have something like this:
from django.shortcuts import render_to_response
from forms import *
import my_quiz_model
def displayQuizForm(request, *args, **kwargs):
if request.method == 'GET':
# Create an empty Quiz object.
# Alternately you can run a query to edit an existing object.
quiz = Quiz()
form = QuizForm(instance=Quiz)
# Render the template and pass the form object along to it.
return render_to_response('form_template.html',{'form': form})
elif request.method == 'POST' and request.POST.get('action') == 'Save':
form = Quiz(request.POST, instance=account)
form.save()
return HttpResponseRedirect("http://example.com/myapp/confirmsave")
Finally your template would look like this:
My Quiz Form