I am using model formsets to add multiple instances of a model at once. and I am using class based views. This is my views.py part for creating a "Library"
class LibraryCreate(View):
model = Library
def post(self, request, *args, **kwargs):
LibraryFormSet = modelformset_factory(
Library, form=create_library, extra=2)
if request.method == 'POST':
formset = LibraryFormSet(request.POST, request.FILES)
if formset.is_valid():
# do something with the formset.cleaned_data
pass
else:
formset = LibraryFormSet()
return render_to_response(
'trial1/library_form.html', {'formset': formset})
def get(self, request, *args, **kwargs):
LibraryFormSet = modelformset_factory(
Library, form=create_library, extra=2)
formset = LibraryFormSet(queryset=Library.objects.none())
return render_to_response(
'trial1/library_form.html', {'formset': formset})
and this is my template
<form method="post" action="{% url "library_create" %}">
{% csrf_token %}
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
<input type="submit" value="create" />
now for some reason when I try to submit the form it returns a 403 forbidden because "CSRF token missing or incorrect.". I don't get why this is not working and its getting really frustrating.
Use
render
instead ofrender_to_response
so that the request is included in the template context.