def test_validate_type_ok(self): form = ContactForm({ 'user': self.user.pk, 'kind': self.label.pk, 'value': '+01234567890', }) self.assertTrue(form.is_valid())
def contactview(request): form = ContactForm() if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): form.save(commit = True) print('details saved successfully') return render(request, 'testapp/thanks.html') return render(request, 'testapp/contact.html', {'form':form})
def test_validate_type(self): form = ContactForm({ 'user': self.user.pk, 'kind': self.label.pk, 'value': 'abc', }) self.assertFalse(form.is_valid()) self.assertIn('value', form.errors) self.assertEqual(form.errors['value'], [six.text_type(form.error_messages['wrong_type'])])
def contact_view(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): form.save() form = ContactForm() return render(request, 'contact.html', {'form': form}) else: return HttpResponse('<h1> Invalid Form</h1>') else: form = ContactForm() return render(request, 'contact.html', {'form': form})
def test_label_required(self): label = ContactClassifierLabelFactory(required=True) form = ContactForm({ 'user': self.user.pk, 'kind': label.pk, 'value': None, }) self.assertFalse(form.is_valid()) self.assertIn('value', form.errors) self.assertEqual( form.errors['value'], [six.text_type(form.fields['value'].error_messages['required'])])
def contact_view(request): if request.method == 'POST': form = ContactForm(request.POST) if form.is_valid(): name1 = request.POST.get('name') salary1 = request.POST['salary'] email1 = form.cleaned_data['email'] location1 = form.cleaned_data.get('location') data = ContactData(name=name1, salary=salary1, email=email1, location=location1) data.save() form = ContactForm() return render(request, 'contact.html', {'form': form}) else: return HttpResponse('<h1> Invalid Form</h1>') else: form = ContactForm() return render(request, 'contact.html', {'form': form})