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)