I have a model Organization with two fields 'id' and 'name'. I intend to populate it using dynamic model formsets.The code I have this far is as follows.
forms
class OrganizationForm(forms.ModelForm):
class Meta:
model = Organization
fields = ('name',)
OrganizationFormset = modelformset_factory(Organization, form=OrganizationForm, fields=('name', ), extra=1)
views
class OrganizationCreate(CreateView):
model = Organization
form_class = OrganizationForm
def get_context_data(self, **kwargs):
context = super(OrganizationCreate, self).get_context_data(**kwargs)
context['formset'] = OrganizationFormset()
return context
def post(self, request, *args, **kwargs):
formset = OrganizationFormset(request.POST)
if formset.is_valid():
return self.form_valid(formset)
def form_valid(self, formset):
formset.save()
return HttpResponseRedirect('/')
def form_invalid(self, formset):
return self.render_to_response(self.get_context_data(formset=formset))
class OrganizationUpdate(UpdateView):
model = Organization
form_class = OrganizationForm
template_name_suffix = '_update_form'
def get_context_data(self, **kwargs):
context = super(OrganizationUpdate, self).get_context_data(**kwargs)
context['formset'] = OrganizationFormset()
return context
def post(self, request, *args, **kwargs):
formset = OrganizationFormset(request.POST)
if formset.is_valid():
return self.form_valid(formset)
def form_valid(self, formset):
formset.save()
return HttpResponseRedirect('/')
def form_invalid(self, formset):
return self.render_to_response(self.get_context_data(formset=formset))
template create
<form id="myForm" method="post" action="">
{% csrf_token %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
{{ form }}
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
<tr>
<td>
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
{{ form.field1 }}
</td>
<td>{{ form.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ formset.management_form }}
<input type="submit" value="Create Location">
</form>
update
<form id="myForm" method="post" action="">
{% csrf_token %}
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %}
<tr>
<td>
{% if form.instance.pk %}{{ form.DELETE }}{% endif %}
{{ form.field1 }}
</td>
<td>{{ form.name }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ formset.management_form }}
<input type="submit" value="Update organizations">
</form>
However, both the create template and the update template display pre-existing organizations while this should only be happening in the update template. Moreover, deleting pre-existing organizations in the update page returns the following error:
The view aims.views.OrganizationUpdate didn't return an HttpResponse object. It returned None instead.
I am new to django generic views and working with formsets. What I'm I doing wrong?
Thanks in advance.