create route form fixed
This commit is contained in:
SDE
2023-07-31 17:56:54 +03:00
parent 70ff3841f6
commit b480f8a239
4 changed files with 46 additions and 41 deletions

View File

@@ -20,14 +20,25 @@ class CreateRouteForm(forms.ModelForm):
# print('check')
cleaned_data = super(CreateRouteForm, self).clean()
data = dict(self.data)
if 'from_place' in data and data['from_place'] not in [item[0] for item in self.fields['from_place'].choices]:
self.cleaned_data['from_place'] = transfer_location_choices[0][0]
if 'to_place' in data and data['to_place'] not in [item[0] for item in self.fields['to_place'].choices]:
self.cleaned_data['to_place'] = transfer_location_choices[0][0]
if 'cargo_type' in data and data['cargo_type'] not in [item[0] for item in self.fields['cargo_type'].choices]:
self.cleaned_data['cargo_type'] = cargo_type_choices[0][0]
try:
if 'from_place' in self.data and self.data['from_place'] not in [item[0] for item in self.fields['from_place'].choices]:
cleaned_data['from_place'] = self.fields['from_place'].choices[0][0]
if 'from_place' in self.errors and self.errors['from_place'].data[0].code == 'invalid_choice':
del self.errors['from_place']
if 'to_place' in self.data and self.data['to_place'] not in [item[0] for item in self.fields['to_place'].choices]:
cleaned_data['to_place'] = self.fields['to_place'].choices[0][0]
if 'to_place' in self.errors and self.errors['to_place'].data[0].code == 'invalid_choice':
del self.errors['to_place']
if 'cargo_type' in self.data and self.data['cargo_type'] not in [item[0] for item in self.fields['cargo_type'].choices]:
cleaned_data['cargo_type'] = self.fields['cargo_type'].choices[0][0]
if 'cargo_type' in self.errors and self.errors['cargo_type'].data[0].code == 'invalid_choice':
del self.errors['cargo_type']
except Exception as e:
print(str(e))
# reg_user = check_authorizationBy_cleaned_data(cleaned_data)
# # print(reg_user)

View File

@@ -1,3 +1,5 @@
import json
from django.shortcuts import render
from uuid import uuid1
@@ -25,8 +27,12 @@ def new_route_view_ajax(request):
}
try:
data = request.POST
errors_off = True
data = request.POST
if not data and request.body:
data = json.loads(request.body)
# show_errors = False
if 'type_transport' in data:
@@ -45,14 +51,6 @@ def new_route_view_ajax(request):
('letter', _('Письмо\Документ'))
)
# form.fields['from_place'].choices = transfer_location_choices
# form.fields['to_place'].choices = transfer_location_choices
# form.fields['cargo_type'].choices = cargo_type_choices
#
# form = CreateRouteForm(data)
#
# if not form.is_valid():
# pass
else:
transfer_location_choices = (
@@ -77,27 +75,18 @@ def new_route_view_ajax(request):
form = CreateRouteForm(data)
if not form.is_valid():
pass
Dict = {
'form': form
'form': form,
'errors_off': errors_off
}
# print(form)
except Exception as e:
form.errors.append({'__all__': f'Ошибка: {str(e)}'})
# form.errors.append({'__all__': f'Ошибка: {str(e)}'})
print(str(e))
html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request)
return JsonResponse({'html': html}, status=200)

View File

@@ -262,6 +262,10 @@ function OnSelectionChange(el) {
let form = el.form;
let formData = new FormData(form);
// let data = {};
// data['csrfmiddlewaretoken'] = $('input[name="csrfmiddlewaretoken"]')[0].value;
// data['type_transport'] = $('select[name="type_transport"]')[0].value;
$.ajax({
headers: {"X-CSRFToken": $('input[name=csrfmiddlewaretoken]').val()},
url: '/ru/user_account/new_route_view/',
@@ -271,6 +275,7 @@ function OnSelectionChange(el) {
processData: false,
contentType: false,
// enctype: 'json',
// data: JSON.stringify(data),
data: formData,
success: function (data) {
console.log('data.html');

View File

@@ -24,7 +24,7 @@
</option>
{% endfor %}
</select>
{% if form.errors and form.errors.type_transport %}
{% if not errors_off and form.errors and form.errors.type_transport %}
<span>{{ form.errors.type_transport }}</span>
{% endif %}
</div>
@@ -43,7 +43,7 @@
{% if form.data.departure_DT %}value="{{ form.data.departure_DT }}"{% endif %}
/>
{% if form.errors and form.errors.departure_DT %}
{% if not errors_off and form.errors and form.errors.departure_DT %}
<span>{{ form.errors.departure_DT }}</span>
{% endif %}
</div>
@@ -57,7 +57,7 @@
{% if form.data.arrival_DT %}value="{{ form.data.arrival_DT }}"{% endif %}
{# |date: 'Y-m-d H:i' #}
/>
{% if form.errors and form.errors.arrival_DT %}
{% if not errors_off and form.errors and form.errors.arrival_DT %}
<span>{{ form.errors.arrival_DT }}</span>
{% endif %}
</div>
@@ -84,7 +84,7 @@
<div class="input_list" name="from_address_point_txt_list">
</div>
{% if form.errors and form.errors.from_address_point %}
{% if not errors_off and form.errors and form.errors.from_address_point %}
<span>{{ form.errors.from_address_point}}</span>
{% endif %}
</div>
@@ -105,7 +105,7 @@
<div class="input_list" name="to_address_point_txt_list">
</div>
{% if form.errors and form.errors.to_address_point %}
{% if not errors_off and form.errors and form.errors.to_address_point %}
<span>{{ form.errors.to_address_point }}</span>
{% endif %}
</div>
@@ -133,7 +133,7 @@
</option>
{% endfor %}
</select>
{% if form.errors and form.errors.from_place %}
{% if not errors_off and form.errors and form.errors.from_place %}
<span>{{ form.errors.from_place }}</span>
{% endif %}
</div>
@@ -150,7 +150,7 @@
</option>
{% endfor %}
</select>
{% if form.errors and form.errors.to_place %}
{% if not errors_off and form.errors and form.errors.to_place %}
<span>{{ form.errors.to_place }}</span>
{% endif %}
</div>
@@ -194,7 +194,7 @@
{# <lable for="">#}
{##}
{# </lable>#}
{% if form.errors and form.errors.cargo_type %}
{% if not errors_off and form.errors and form.errors.cargo_type %}
<span>{{ form.errors.cargo_type }}</span>
{% endif %}
</div>
@@ -212,7 +212,7 @@
{# value=""#}
{% if form.cleaned_data.weight %}value="{{ form.cleaned_data.weight}}"{% endif %}
/>
{% if form.errors and form.errors.weight %}
{% if not errors_off and form.errors and form.errors.weight %}
<span>{{ form.errors.weight }}</span>
{% endif %}
</div>
@@ -231,7 +231,7 @@
{% if form.cleaned_data.phone %}value="{{ form.cleaned_data.phone}}"{% endif %}
/>
{% if form.errors and form.errors.phone %}
{% if not errors_off and form.errors and form.errors.phone %}
<span>{{ form.errors.phone }}</span>
{% endif %}
</div>
@@ -246,7 +246,7 @@
placeholder="{{ form.fields.extra_phone.label }}"
{% if form.cleaned_data.extra_phone %}value="{{ form.cleaned_data.extra_phone}}"{% endif %}
/>
{% if form.errors and form.errors.extra_phone %}
{% if not errors_off and form.errors and form.errors.extra_phone %}
<span>{{ form.errors.extra_phone }}</span>
{% endif %}
</div>
@@ -257,7 +257,7 @@
<div>
<label for="id_receive_msg_by_email">{{ form.fields.receive_msg_by_email.label }}</label>
<input type="checkbox" name="receive_msg_by_email" id="id_receive_msg_by_email">
{% if form.errors and form.errors.receive_msg_by_email %}
{% if not errors_off and form.errors and form.errors.receive_msg_by_email %}
<span>{{ form.errors.receive_msg_by_email }}</span>
{% endif %}
</div>