Валидация полей
This commit is contained in:
parent
4a538a564a
commit
6953938752
@ -1,15 +1,29 @@
|
||||
from datetime import datetime
|
||||
|
||||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
|
||||
class ReportForm(forms.Form):
|
||||
date_start = forms.DateField(
|
||||
widget=forms.TextInput(
|
||||
attrs={'class': 'form-control date-picker'}), label="Период:"
|
||||
attrs={'class': 'form-control date-picker'}), label="Дата начала периода"
|
||||
)
|
||||
date_end = forms.DateField(
|
||||
widget=forms.TextInput(
|
||||
attrs={'class': 'form-control date-picker'}), label=""
|
||||
attrs={'class': 'form-control date-picker'}), label="Дата конца периода"
|
||||
)
|
||||
|
||||
# def is_valid(self):
|
||||
# return super().is_valid()
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
|
||||
date_start = cleaned_data.get("date_start")
|
||||
date_end = cleaned_data.get("date_end")
|
||||
|
||||
if not date_start or not date_end:
|
||||
raise ValidationError("Оба поля даты должны быть заполнены")
|
||||
|
||||
if date_end < date_start:
|
||||
raise ValidationError("Дата конца раньше даты начала")
|
||||
|
||||
return cleaned_data
|
||||
|
||||
@ -21,16 +21,38 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<div class="col-12">
|
||||
<form method="post">
|
||||
<div class="d-flex">
|
||||
{{ form.date_start.label }}
|
||||
{{ form.date_start }}
|
||||
-
|
||||
{{ form.date_end }}
|
||||
{% csrf_token %}
|
||||
<div class="card mt-3">
|
||||
<h5 class="card-header">Отчет</h5>
|
||||
<div class="card-body">
|
||||
<form method="post">
|
||||
{% if form.errors %}
|
||||
{% for error in form.non_field_errors %}
|
||||
<div class="alert alert-danger alert-dismissible">
|
||||
<h5><i class="icon fas fa-ban"></i>Ошибка ввода</h5>
|
||||
{{ error }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
<div class="mb-3">
|
||||
<div class="mb-2">
|
||||
<label for="{{ form.date_start.id_for_label }}" style="user-select: none">
|
||||
{{ form.date_start.label }}:
|
||||
</label>
|
||||
{{ form.date_start }}
|
||||
</div>
|
||||
<div class="mb-2">
|
||||
<label for="{{ form.date_end.id_for_label }}" style="user-select: none">
|
||||
{{ form.date_end.label }}:
|
||||
</label>
|
||||
{{ form.date_end }}
|
||||
</div>
|
||||
</div>
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn btn-primary">Сформировать отчет</button>
|
||||
</form>
|
||||
</div>
|
||||
<button type="submit">Отчет</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -7,6 +7,7 @@ from report.forms.form_report import ReportForm
|
||||
class ReportView(LoginRequiredMixin, FormView):
|
||||
template_name = 'report/report.html'
|
||||
form_class = ReportForm
|
||||
success_url = '/'
|
||||
|
||||
def form_valid(self, form):
|
||||
return ...
|
||||
return super().form_valid(form)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user