diff --git a/RoutesApp/forms.py b/RoutesApp/forms.py index 9335dd7..5f8584b 100644 --- a/RoutesApp/forms.py +++ b/RoutesApp/forms.py @@ -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) diff --git a/RoutesApp/js_views.py b/RoutesApp/js_views.py index 74cbb58..3af5e42 100644 --- a/RoutesApp/js_views.py +++ b/RoutesApp/js_views.py @@ -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) diff --git a/static/js/user_profile.js b/static/js/user_profile.js index 89188a9..f404572 100644 --- a/static/js/user_profile.js +++ b/static/js/user_profile.js @@ -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'); diff --git a/templates/blocks/profile/b_new_route.html b/templates/blocks/profile/b_new_route.html index 981a466..a970ed3 100644 --- a/templates/blocks/profile/b_new_route.html +++ b/templates/blocks/profile/b_new_route.html @@ -24,7 +24,7 @@ {% endfor %} - {% if form.errors and form.errors.type_transport %} + {% if not errors_off and form.errors and form.errors.type_transport %} {{ form.errors.type_transport }} {% endif %} @@ -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 %} {{ form.errors.departure_DT }} {% endif %} @@ -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 %} {{ form.errors.arrival_DT }} {% endif %} @@ -84,7 +84,7 @@