def manage_death_notice(request, death_notice_id=None): if request.method == 'POST': if request.POST.has_key('delete_death_notice'): Death_notice.objects.filter(funeral_home__username=request.user.username).get(pk=death_notice_id).delete() msg = ugettext('The %(verbose_name)s was deleted.') %\ { 'verbose_name': Death_notice._meta.verbose_name } messages.success(request, msg, fail_silently=True) return HttpResponseRedirect(reverse('death_notice_index')) if death_notice_id: death_notice = Death_notice.objects.get(pk=death_notice_id) form = Death_noticeForm(request.POST, request.FILES, instance=death_notice) formset = ServiceFormSet(request.POST, instance=death_notice) dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST, instance=death_notice) else: form = Death_noticeForm(request.POST, request.FILES) formset = ServiceFormSet(request.POST) dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST) if form.is_valid() and formset.is_valid() and dn_os_formset.is_valid(): death_notice = form.save(commit=False) death_notice.funeral_home = request.user death_notice.save() formset = ServiceFormSet(request.POST, instance=death_notice) formset.save() dn_os_formset = DeathNoticeOtherServicesFormSet(request.POST, instance=death_notice) msg = ugettext('Death notice for %s %s saved.' % (death_notice.first_name, death_notice.last_name)) messages.success(request, msg, fail_silently=True) dn_os_formset.save() if request.POST.has_key('add_another'): return HttpResponseRedirect(reverse('add_death_notice')) else: return HttpResponseRedirect(reverse('death_notice_index')) else: if death_notice_id: death_notice = Death_notice.objects.get(pk=death_notice_id) form = Death_noticeForm(instance=death_notice) formset = ServiceFormSet(instance=death_notice) dn_os_formset = DeathNoticeOtherServicesFormSet(instance=death_notice) else: form = Death_noticeForm() formset = ServiceFormSet(instance=Death_notice()) dn_os_formset = DeathNoticeOtherServicesFormSet(instance=Death_notice()) response_dict = { 'form': form, 'formset': formset, 'other_services_formset': dn_os_formset, 'death_notice_id': death_notice_id, } # To be used for Preview if death_notice_id: response_dict['object_list'] = [death_notice] t = loader.get_template('manage_death_notice.html') c = RequestContext(request, response_dict) data = t.render(c) r = HttpResponse(data) return r return render_to_response('manage_death_notice.html', response_dict, context_instance=RequestContext(request))