def edit_contact(request, contact_id): contact = Contact.objects.get(pk=contact_id) AddressInlineFormSet = inlineformset_factory(Contact, Address, form=AddressForm, extra=1) TelephoneInlineFormSet = inlineformset_factory(Contact, Telephone, extra=1) if request.method == "POST": form = ContactForm(request.POST, instance=contact) address_formset = AddressInlineFormSet(request.POST, request.FILES, instance=contact) telephone_formset = TelephoneInlineFormSet(request.POST, request.FILES, instance=contact) if form.is_valid() and address_formset.is_valid( ) and telephone_formset.is_valid(): form.save() address_formset.save() telephone_formset.save() return HttpResponseRedirect( reverse('contacts.views.detail', args=(contact.pk, ))) else: form = ContactForm(instance=contact) address_formset = AddressInlineFormSet(instance=contact) telephone_formset = TelephoneInlineFormSet(instance=contact) return render_to_response( "contacts/form.html", { "form": form, "address_formset": address_formset, "telephone_formset": telephone_formset, }, RequestContext(request))
def addcontact(request): if request.method == 'POST': # If the form has been submitted... form = ContactForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass print(form.cleaned_data) print(request.POST['name']) form.save() return HttpResponseRedirect('/contacts/listcontacts') # Redirect after POST else: form = ContactForm() # An unbound form return render(request, 'contacts/addcontactform.html', { 'form': form, })
def addcontact(request): if request.method == 'POST': # If the form has been submitted... form = ContactForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass print(form.cleaned_data) print(request.POST['name']) form.save() return HttpResponseRedirect( '/contacts/listcontacts') # Redirect after POST else: form = ContactForm() # An unbound form return render(request, 'contacts/addcontactform.html', { 'form': form, })
def new_contact(request): print "new_contact" AddressInlineFormSet = inlineformset_factory(Contact, Address, form=AddressForm, extra=1) TelephoneInlineFormSet = inlineformset_factory(Contact, Telephone, extra=1) if request.method == "POST": form = ContactForm(request.POST) address_formset = AddressInlineFormSet(request.POST, request.FILES) telephone_formset = TelephoneInlineFormSet(request.POST, request.FILES) if form.is_valid() and address_formset.is_valid( ) and telephone_formset.is_valid(): contact = form.save() addresses = address_formset.save(commit=False) for a in addresses: a.contact_id = contact.pk a.save() phones = telephone_formset.save(commit=False) for p in phones: p.contact_id = contact.pk p.save() address_formset.save_m2m() telephone_formset.save_m2m() return HttpResponseRedirect( reverse('contacts.views.detail', args=(contact.pk, ))) else: form = ContactForm() address_formset = AddressInlineFormSet() telephone_formset = TelephoneInlineFormSet() return render_to_response( "contacts/form.html", { "form": form, "address_formset": address_formset, "telephone_formset": telephone_formset, }, RequestContext(request))