def test_form(self): form_data = { 'name':'Ivan', 'surname': 'Petrov', 'date_of_birth':'1990-02-05', 'email': '*****@*****.**', 'phone': '0235326223', 'bio': 'bio info' } form = PersonForm(data=form_data) self.assertEqual(form.is_valid(), True) form_data['email'] = 'email.google.com' form = PersonForm(data=form_data) self.assertEqual(form.is_valid(), False)
def update_person(request, person_id): person = get_object_or_404(Person, pk=person_id) form = PersonForm(request.POST or None, instance=person) if form.is_valid(): update = form.save(commit=False) update.save() return HttpResponseRedirect('/personlist/') context = {'form': form, 'person': person} return render(request, 'task/person_select.html', context)
def new_person(request): if request.method == 'POST': form = PersonForm(request.POST) if form.is_valid(): new = form.save(commit=False) new.save() return HttpResponseRedirect('/personlist/') else: form = PersonForm() context = {'form': form} return render(request, 'task/new_person.html', context)
def edit_person(request): person = Person.objects.get(name="Roman", surname="Karpovych", email="*****@*****.**") if request.method == "GET": form = PersonForm(instance=person) if request.method == "POST": form = PersonForm(data=request.POST, instance=person) if form.is_valid(): cd = form.cleaned_data posted_person = Person.objects.get(name=cd['name'], surname=cd['surname']) if request.FILES: form = PersonForm(data=request.POST, files=request.FILES) form.data['photo'] = settings.MEDIA_URL + "photos/" + str(request.FILES['photo']) else: form.data['photo'] = person.photo if posted_person: form = PersonForm(data=request.POST, files=request.FILES, instance=posted_person) form.save() return HttpResponseRedirect("") return render_to_response('person_form.html', {'form': form}, context_instance=RequestContext(request))