1

I'm submitting formset using ajax, It was working fine and getting response. I made a filter to my template as this answer the error shows up. for that I removed the filter completely from the project and re-run the server and the error still exist. I wan't to know how to troubleshoot and know what causes the error instead of making a lot of changes (try and error method) to my code and blowup my project :D

I'm using VS Code on Windows 10

urls.py

path('legs/assign/<int:trans_id>', views.legs_assign, name='legs_assign'),

views.py:

def legs_assign(request, trans_id):
    context = {}
    try:
        # handle the request to save data (working fine and records saved)
        # add some context

        print(str(context)) # => works fine and prints the context to console
        html_response = render(request,'my_template.html', context)
        print(srt(html_response)) # => doesn't work or print anything to console
        return HttpResponse(html_response,status=200)
    except Exception as e:
        return HttpResponse('%s %s <P>Somthing went wrong!</p>' % (e,e.args),status=500)

script.js

$(document).ready(function(){
    $(".assign_btn").click(function(){
        var trans_id = $(this).val()
        var form_anchor = "#trans_legs_formset_" + trans_id
        var serializedData = $(form_anchor).serialize();

        $.ajax({
            url: $(form_anchor).data('url'),
            data: serializedData,
            type: 'POST',
            success: function(response){
                $(form_anchor).html(response);
            },
        })
    });
});

my_template.html

<form method="POST" id="trans_legs_formset_1" data-url="{% url 'legs_assign' 1 %}">
  {% csrf_token %}
  {{ legs_formset.management_form }}
  {% for form in my_formset %}
     {% for field in form.visible_fields %}
          {{ field }}
     {% endfor %}
     {% for field in form.hidden_fields %}
          <div style="display:none;">{{ field }}</div>
     {% endfor %}
  {% endfor %}
<form>

Python console error:

Internal Server Error: /legs/assign/1

Browser console error:

enter image description here

Poula Adel
  • 609
  • 1
  • 10
  • 33

0 Answers0