walidacja formularza

This commit is contained in:
Hokan 2019-01-22 18:39:08 +01:00
parent 531adcee2e
commit e36992bc3f
4 changed files with 21 additions and 7 deletions

Binary file not shown.

View File

@ -8,6 +8,7 @@
</head>
<body>
<form action="" method="post">
{{ error }}
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">

View File

@ -3,6 +3,7 @@ from django.template import loader
from django.shortcuts import get_object_or_404, render
from .models import *
from .forms import *
import re
# Create your views here.
@ -29,12 +30,24 @@ def reservation(request):
def addclient(request):
if request.method == "GET":
form = ClientForm()
template = loader.get_template('form.html')
return HttpResponse(template.render({'form': form}, request))
elif request.method == "POST":
error = ""
if request.method == "POST":
client = ClientForm(request.POST)
client.save()
return HttpResponse()
if client.is_valid():
if check(client.cleaned_data["id_number"]):
client.save()
return HttpResponse()
error = "Numer dowodu nieprawidłowy"
form = ClientForm()
template = loader.get_template('form.html')
return HttpResponse(template.render({'form': form, 'error': error}, request))
def check(numer):
pattern = re.compile(r'[A-Z][A-Z][A-Z]\d\d\d\d\d\d')
return pattern.match(numer)