Answer the question
In order to leave comments, you need to log in
Django CBV ajax implementation?
There is an example of ajax processing.
class AjaxableResponseMixin(object):
"""
Mixin to add AJAX support to a form.
Must be used with an object-based FormView (e.g. CreateView)
"""
def form_invalid(self, form):
response = super(AjaxableResponseMixin, self).form_invalid(form)
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
else:
return response
def form_valid(self, form):
# We make sure to call the parent's form_valid() method because
# it might do some processing (in the case of CreateView, it will
# call form.save() for example).
response = super(AjaxableResponseMixin, self).form_valid(form)
if self.request.is_ajax():
data = {
'pk': self.object.pk,
}
return JsonResponse(data)
else:
return response
class CallBacksCreate(AjaxableResponseMixin, CreateView):
model = CallBacks
fields = ['phone']
Answer the question
In order to leave comments, you need to log in
Tell me, it turns out we send from the form via js via POST to the URL to which CallBacksCreate is attachedYes. Not only via js, but also just a http request
Is it an example of a complete implementation, or before going to bed it’s hard to get ...did you provide the complete implementation?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question