iommirocks / iommi

Your first pick for a django power cord
http://iommi.rocks
BSD 3-Clause "New" or "Revised" License
804 stars 51 forks source link

Query | Leverage Ajax based form save #549

Closed akhileshThapliyal closed 3 months ago

akhileshThapliyal commented 3 months ago

Hi Guys,

I have a multi-step form and I want to save the forms as I proceed further using Ajax and not using the post handler. is it possible, to use a reference code?

Javascript Code

<script>
   $("form[name="profile_info_form"]).submit(function(e) {
     e.preventDefault();

     var csrftoken = jQuery('[name=csrfmiddlewaretoken]').val();
     var form = $("#profile_info_form");

     $.ajax({
       method: form.attr("method"),
       url: form.attr("action"),
       data: {
           csrfmiddlewaretoken: csrftoken,
           form: form.serialize()
       },
       dataType: 'Html',
       success: function (data) {
           $('.form-container').html(data);
       }
   });
</script>

Views.py

@login_required
def save_profile_info(request):
    if request.method == 'POST':
        form = ProfileInfoForm.bind(request=request)
        if form.is_valid():
            form.save()
            return {"status": "success"}
        else:
            return {"status": "fail"}`

Error:

image

Regards, Akhilesh

boxed commented 3 months ago

The view here is returning a dict for some reason. This is not valid django. I believe also that this issue is obsolete, given the discussion on the discord.