fix create route form
This commit is contained in:
SDE
2023-07-31 22:08:29 +03:00
parent 19eb2957e4
commit e5dea6a83c
3 changed files with 323 additions and 16 deletions

View File

@@ -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)