def edit_patient(request, patient_id): form_title = 'Edit Patient Information' patient = get_object_or_404(Patient, pk=patient_id) if request.method == 'POST': form = PersonForm(request.POST, instance=patient) if form.is_valid(): form.save() return HttpResponseRedirect( reverse('patient:view_patient', args=(patient.id, ))) else: form = PersonForm(instance=patient) return render(request, 'patient/create_patient.html', { 'form': form, 'form_title': form_title })
class PersonEditView(TemplateView): template_name = 'person/profile_edit.html' @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): action = request.POST.get('action') self.profile_form = PersonForm( (request.POST if action == 'profile' else None), (request.FILES if action == 'profile' else None), prefix='profile', instance=request.user ) self.password_form = UserPasswordChangeForm(request.user, (request.POST if action == 'password' else None), prefix='password') self.email_form = UserEmailChangeForm(request.user, (request.POST if action == 'email' else None), prefix='email') return super(PersonEditView, self).dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs): context = super(PersonEditView, self).get_context_data(**kwargs) context['profile_form'] = self.profile_form context['password_form'] = self.password_form context['email_form'] = self.email_form return context def post(self, request, *args, **kwargs): if self.profile_form.is_valid(): self.profile_form.save() messages.success(request, ugettext(u'Профиль успешно сохранен.')) return redirect(request.path) elif self.password_form.is_valid(): self.password_form.save() request.user.backend = request.session[BACKEND_SESSION_KEY] login(request, request.user) messages.success(request, ugettext(u'Пароль успешно сохранен.')) return redirect(request.path) elif self.email_form.is_valid(): self.email_form.save() messages.success(request, ugettext(u'Email успешно сохранен.')) return redirect(request.path) return self.get(request, *args, **kwargs)
def edit_person(request): if 'id' not in request.session.keys(): return redirect('/sign_in/') #TODO: check to make sure user logged in is associated with person - person ID may be public in the URL # get person ID from querystring person_id = request.GET['id'] # pass person ID to Person model to pull current person current_person = Person.objects.get(pk=person_id) form = PersonForm(request.POST or None, instance=current_person) if request.POST and form.is_valid(): form.save() # Save was successful redirect_url = '/view_person_list/' return redirect(redirect_url) context = {'form': form} return render(request, 'person/update_person.html', context=context)
def person_edit(request, pk): if pk == 'new': instance = None else: instance = get_object_or_404(Person, pk=pk) form = PersonForm(request.POST or None, instance=instance) if form.is_valid(): person = form.save() messages.success(request, 'Successfully saved!') return redirect('person_edit', pk=person.pk) return render( request, 'person/person_edit.html', { 'main_menu_key': 'persons', 'form': form, }, )
def add_or_edit(request, id): if id is None: person = None page_name = "add" else: person = get_object_or_404(Person, id=id) page_name = "edit" if request.method == "GET": form = PersonForm(instance=person) phone_forms = PhoneNumberFormSet(instance=person) if request.method == "POST": form = PersonForm(request.POST, instance=person) phone_forms = PhoneNumberFormSet(request.POST, instance=person) if form.is_valid() and phone_forms.is_valid(): person = form.save() phone_forms = PhoneNumberFormSet(request.POST, instance=person) phone_forms.is_valid() phone_forms.save() return HttpResponseRedirect('/person/%s/' % person.id) return render_to_response('add.html', {'page_name': page_name, 'form': form, 'phone_forms': phone_forms, 'person': person}, RequestContext(request))