Solution 1 :

It doesn’t explain why I have this problem but I found a workaround to solve my problem.

Instead of dynamically generate forms, I generate them with template and then I hide them all. Later, I make those I need visible when I need thanks to css.

Problem :

I’m building a website (e-commerce like) with Django.

At some point I display a list of items and for each item there is a form with submit button Order and Quantity picker.

I’ve implemented filter function that delete the html code of my items list and rebuild it with the matching items with jquery.

The new forms generated by this function do nothing when the submit button is clicked

Here is a part of the code I use in my function (I use an ajax call to generate a json of matching items and then I display them in the table list) :

$.each(code_json, function(index, value){
                var string = "<tr id="+ value.material +"><td>"+ value.manufNo +"</td><form method='POST' action='/add_to_cart/"+value.material+"/"+ value.node+"/{{language}}'><td><input type='submit' class='btn' value='Commander' style='color:blue;'></td><td><input id='qty' type='number' name='qty' class='form-control' value='1'></td></form></tr>";
                $("#header").after(string);
                });

I know that usually with Django you have to add {% csrf_token %} for each form. However it throw an error page usually when missing and here it doesn’t show anything

Edit : I tried to bind an onclick event on the submit button dynamically created. In this I did a $.post in jquery to simulate the submit of the form but nothing happend

$(document).on('click', '.btnStandard', function(event) {
        console.log($(this).attr('id'));
        $.post('/add_to_cart/'+$(this).attr('id'),
        {
            qty: $("#qty"+$(this).attr('id')).val()
        },function(data,status,xhr){
            alert("Data : "+data+", status: "+status+", xhr: "+xhr);
        });

It print in console $(this).attr(‘id’) but it doesn’t do anything else
Thank you for your help

Comments

Comment posted by Dynamic form not submitting (jQuery)

Does this answer your question?

Comment posted by LoP Castro

Thank you but I don’t use javascript for for the submit … I just use the action field in form tag. I would like not to complicate it overstep.

Comment posted by Lapskaus

The issue remains the same afaik. I am quite sure that you

Comment posted by LoP Castro

Ok I will give a try tomorrow and let u know thanks

Comment posted by LoP Castro

Unfortunately it doesn’t work for me and by reading the post, it wasn’t the solution OP used to solve his problem

By