R
R
Romua1d2016-11-04 00:20:22
Django
Romua1d, 2016-11-04 00:20:22

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']

Tell me, it turns out we send from the form via js via POST to the URL to which CallBacksCreate is attached, or, somehow, it works differently. Is there an example of a complete implementation, or is it hard to get to bed before going to bed ...
Thank you in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-11-04
@Romua1d

Tell me, it turns out we send from the form via js via POST to the URL to which CallBacksCreate is attached
Yes. 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 question

Ask a Question

731 491 924 answers to any question