Skip to content

Instantly share code, notes, and snippets.

@taranjeet
Created June 12, 2018 06:01
Show Gist options
  • Save taranjeet/879b920e56514af67348da503af2b33c to your computer and use it in GitHub Desktop.
Save taranjeet/879b920e56514af67348da503af2b33c to your computer and use it in GitHub Desktop.
Django Library: Model Formset view code
# views.py :: part 2
from django.shortcuts import render, redirect
from .forms import BookModelFormset
def create_book_model_form(request):
template_name = 'store/create_normal.html'
heading_message = 'Model Formset Demo'
if request.method == 'GET':
# we don't want to display the already saved model instances
formset = BookModelFormset(queryset=Book.objects.none())
elif request.method == 'POST':
formset = BookModelFormset(request.POST)
if formset.is_valid():
for form in formset:
# only save if name is present
if form.cleaned_data.get('name'):
form.save()
return redirect('book_list')
return render(request, template_name, {
'formset': formset,
'heading': heading_message,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment