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> </head>
<body> <body>
<form action="" method="post"> <form action="" method="post">
{{ error }}
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<input type="submit" value="Submit"> <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 django.shortcuts import get_object_or_404, render
from .models import * from .models import *
from .forms import * from .forms import *
import re
# Create your views here. # Create your views here.
@ -29,12 +30,24 @@ def reservation(request):
def addclient(request): def addclient(request):
if request.method == "GET": error = ""
form = ClientForm() if request.method == "POST":
template = loader.get_template('form.html')
return HttpResponse(template.render({'form': form}, request))
elif request.method == "POST":
client = ClientForm(request.POST) client = ClientForm(request.POST)
if client.is_valid():
if check(client.cleaned_data["id_number"]):
client.save() client.save()
return HttpResponse() 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)