Last active
April 1, 2016 10:54
-
-
Save jerweb63/827024aea8dbc56f59219dbccea455ef to your computer and use it in GitHub Desktop.
DJango inlineformset_factory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from .models import Choice, Question | |
from django.forms.models import inlineformset_factory | |
#class QuestionForm(forms.Form): | |
# question_text = forms.CharField(label='Question Text', max_length=100, widget=forms.TextInput(attrs={'class':'vTextField'})) | |
ChoiceFormSet = inlineformset_factory(Question, Choice, fields=['choice_text'], can_delete=False) | |
class QuestionForm(forms.ModelForm): | |
class Meta: | |
model = Question | |
exclude = ('pub_date',) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.core.urlresolvers import reverse | |
from django.views import generic | |
# Create your views here. | |
from django.http import HttpResponse, HttpResponseRedirect | |
from django.template import loader | |
from .models import Choice, Question | |
from .forms import ChoiceFormSet, QuestionForm | |
def index(request): | |
latest_question_list = Question.objects.order_by('-pub_date')[:5] | |
template = loader.get_template('polls/index.html') | |
context = { | |
'latest_question_list': latest_question_list, | |
} | |
return HttpResponse(template.render(context, request)) | |
def detail(request, question_id): | |
return HttpResponse("You're looking at question %s." % question_id) | |
def results(request, question_id): | |
response = "You're looking at the results of question %s." | |
return HttpResponse(response % question_id) | |
def vote(request, question_id): | |
return HttpResponse("You're voting on question %s." % question_id) | |
def detail(request, question_id): | |
return HttpResponse("You're looking at question %s." % question_id) | |
def results(request, question_id): | |
response = "You're looking at the results of question %s." | |
return HttpResponse(response % question_id) | |
def vote(request, question_id): | |
return HttpResponse("You're voting on question %s." % question_id) | |
def question_new(request, template='polls/question_new.html'): | |
if request.method == 'POST': | |
form = QuestionForm(request.POST) | |
if form.is_valid(): | |
question = form.save(commit=False) | |
choice_formset = ChoiceFormSet(request.POST, instance=question) | |
if choice_formset.is_valid(): | |
question.save() | |
choice_formset.save() | |
return HttpResponseRedirect('/success/') | |
else: | |
form = QuestionForm() | |
choice_formset = ChoiceFormSet(instance=Question()) | |
return render(request, template, {'form':form, | |
'choice_formset': choice_formset}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment