diff --git a/RoutesApp/forms.py b/RoutesApp/forms.py index 5f8584b..1b67574 100644 --- a/RoutesApp/forms.py +++ b/RoutesApp/forms.py @@ -6,7 +6,7 @@ from django.core.exceptions import ValidationError from .models import * -class CreateRouteForm(forms.ModelForm): +class RouteForm(forms.ModelForm): from_address_point_txt = forms.CharField(required=True) to_address_point_txt = forms.CharField(required=True) class Meta: @@ -18,7 +18,7 @@ class CreateRouteForm(forms.ModelForm): def clean(self): # print('check') - cleaned_data = super(CreateRouteForm, self).clean() + cleaned_data = super(RouteForm, self).clean() diff --git a/RoutesApp/js_views.py b/RoutesApp/js_views.py index ef29576..417363c 100644 --- a/RoutesApp/js_views.py +++ b/RoutesApp/js_views.py @@ -31,9 +31,9 @@ def edit_route_ajax(request): msg = f'Недостаточно данных' return JsonResponse({'errors': msg}) - route = Route.objects.get(data['route_id']) + route = Route.objects.get(id=data['route_id']) - form = CreateRouteForm(route) + form = CreateRouteForm(instance=route) Dict = { 'form': form @@ -41,9 +41,11 @@ def edit_route_ajax(request): except Exception as e: # form.errors.append({'__all__': f'Ошибка: {str(e)}'}) - print(str(e)) + msg = f'Ошибка edit_route = {str(e)}' + print(msg) + return JsonResponse({'errors': msg}) - html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request) + html = render_to_string('blocks/profile/b_new_route2.html', Dict, request=request) return JsonResponse({'html': html}, status=200) @@ -52,7 +54,7 @@ def new_route_view_ajax(request): if request.method != 'POST': raise Http404 - form = CreateRouteForm() + form = RouteForm() Dict = { 'form': form } @@ -60,11 +62,20 @@ def new_route_view_ajax(request): errors_off = True - data = request.POST + data = request.POST.dict() if not data and request.body: data = json.loads(request.body) # show_errors = False + if 'from_address_point' in data: + del data['from_address_point'] + if 'from_address_point_txt' in data: + del data['from_address_point_txt'] + + if 'to_address_point' in data: + del data['to_address_point'] + if 'to_address_point_txt' in data: + del data['to_address_point_txt'] if 'type_transport' in data: if data['type_transport'] == 'avia': @@ -96,6 +107,11 @@ def new_route_view_ajax(request): ('letter', _('Письмо\Документ')) ) + form = RouteForm(data) + if not form.is_valid(): + pass + form = RouteForm(initial=form.cleaned_data) + form.fields['from_place'].choices = transfer_location_choices form.fields['to_place'].choices = transfer_location_choices form.fields['cargo_type'].choices = cargo_type_choices @@ -104,10 +120,10 @@ def new_route_view_ajax(request): form.base_fields['to_place'].choices = transfer_location_choices form.base_fields['cargo_type'].choices = cargo_type_choices - form = CreateRouteForm(data) + # form = CreateRouteForm(initial=data) - if not form.is_valid(): - pass + # if not form.is_valid(): + # pass Dict = { 'form': form, @@ -119,7 +135,7 @@ def new_route_view_ajax(request): # form.errors.append({'__all__': f'Ошибка: {str(e)}'}) print(str(e)) - html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request) + html = render_to_string('blocks/profile/b_new_route2.html', Dict, request=request) return JsonResponse({'html': html}, status=200) @@ -163,10 +179,14 @@ def create_route_ajax(request): data = request.POST - form = CreateRouteForm(data) + # obj = Route(data) + # form = RouteForm(data=data, instance=obj) + + form = RouteForm(data) if not form.is_valid(): + form.initial = form.cleaned_data Dict = {'form': form} - html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request) + html = render_to_string('blocks/profile/b_new_route2.html', Dict, request=request) return JsonResponse({'html': html}, status=400) obj = form.save(commit=False) @@ -178,7 +198,7 @@ def create_route_ajax(request): if 'errors' in routes_Dict: form.errors.update(routes_Dict['errors']) Dict = {'form': form} - html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request) + html = render_to_string('blocks/profile/b_new_route2.html', Dict, request=request) return JsonResponse({'html': html}, status=400) html = render_to_string('blocks/profile/b_my_routes.html', routes_Dict, request=request) @@ -197,5 +217,5 @@ def create_route_ajax(request): } } Dict = {'form': errors_Dict} - html = render_to_string('blocks/profile/b_new_route.html', Dict, request=request) + html = render_to_string('blocks/profile/b_new_route2.html', Dict, request=request) return JsonResponse({'html': html}, status=400) \ No newline at end of file diff --git a/templates/blocks/profile/b_new_route2.html b/templates/blocks/profile/b_new_route2.html new file mode 100644 index 0000000..2768433 --- /dev/null +++ b/templates/blocks/profile/b_new_route2.html @@ -0,0 +1,287 @@ +{% load static %} + + + + +
+ {% csrf_token %} +
+ +
+
+ + + {% if not errors_off and form.errors and form.errors.type_transport %} + {{ form.errors.type_transport }} + {% endif %} +
+ +{% if form.initial and form.initial.type_transport and form.initial.type_transport != '' %} +
+
+
+ + + + + {% if not errors_off and form.errors and form.errors.departure_DT %} + {{ form.errors.departure_DT }} + {% endif %} +
+
+ + + {% if not errors_off and form.errors and form.errors.arrival_DT %} + {{ form.errors.arrival_DT }} + {% endif %} +
+
+
+
+
+ + + + + + + +
+
+ {% if not errors_off and form.errors and form.errors.from_address_point %} + {{ form.errors.from_address_point}} + {% endif %} +
+
+ + + +
+ +
+ {% if not errors_off and form.errors and form.errors.to_address_point %} + {{ form.errors.to_address_point }} + {% endif %} +
+
+{#
#} +{# #} +{# #} +{#
#} +{#
#} +{# #} +{# #} +{#
#} +
+
+ + + {% if not errors_off and form.errors and form.errors.from_place %} + {{ form.errors.from_place }} + {% endif %} +
+
+ + + {% if not errors_off and form.errors and form.errors.to_place %} + {{ form.errors.to_place }} + {% endif %} +
+
+ +
+ +
+ +
+{#
#} +{# #} +{# {% for item in form.fields.cargo_type.choices %}#} +{# {{ item.1 }}#} +{# #} +{# {% endfor %}#} +{##} +{# #} + +{#
#} + +
+ {% for item in form.fields.cargo_type.choices %} + {{ item.1 }} + {% endfor %} + +{# #} +{##} +{# #} + {% if not errors_off and form.errors and form.errors.cargo_type %} + {{ form.errors.cargo_type }} + {% endif %} +
+ + +
+ +
+ + + {% if not errors_off and form.errors and form.errors.weight %} + {{ form.errors.weight }} + {% endif %} +
+ + +{#
#} +{# Цена#} +{#
#} +{#
#} +{#
#} +{# от#} +{# #} +{#
#} +{##} +{#
#} +{# до#} +{# #} +{#
#} +{#
#} +{#
#} + +
+ +
+
+ + + {% if not errors_off and form.errors and form.errors.phone %} + {{ form.errors.phone }} + {% endif %} +
+ +
+ + + + {% if not errors_off and form.errors and form.errors.extra_phone %} + {{ form.errors.extra_phone }} + {% endif %} +
+
+ +
+ +
+ + + {% if not errors_off and form.errors and form.errors.receive_msg_by_email %} + {{ form.errors.receive_msg_by_email }} + {% endif %} +
+
+ +
+ + +{% endif %} + +
\ No newline at end of file