Answer the question
In order to leave comments, you need to log in
ajax handling in cbv how?
def form_invalid(self, form):
if self.request.is_ajax():
data = {
'code': 0,
'message': form.errors
}
return JsonResponse(data)
else:
response = super(AjaxableResponseMixin, self).form_invalid(form)
return response
def form_valid(self, form):
form.instance.subject = "Заявка перезвонить № " + str(random.randint(1, 999999))
form.instance.type = "1"
if form.instance.phone == '':
if self.request.is_ajax():
data = {
'code': 0,
'message': 'Проверьте информацию',
}
return JsonResponse(data)
else:
response = super(AjaxableResponseMixin, self).form_valid(form)
if self.request.is_ajax():
data = {
'code': 1,
'message': 'Успешно отправлено',
}
return JsonResponse(data)
else:
return response
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question