Created
June 12, 2018 06:01
-
-
Save taranjeet/879b920e56514af67348da503af2b33c to your computer and use it in GitHub Desktop.
Django Library: Model Formset view code
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
# 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