Django: Dynamic jquery form not working when {% csrf_token %} is added
NickName:Nelson Kshetrimayum Ask DateTime:2021-04-19T14:49:16

Django: Dynamic jquery form not working when {% csrf_token %} is added

I am using a dynamic form in my django template using jquery but when I add submit button or csrf token to the form the dynamic form stops working and I can not add new fields anymore.

Here is my html file

<html>
<body>

  <form method="post">

    <p>
      <label>Name:</label> <input type="text">
      <label>Number:</label> <input type="number">
      <span class="remove">Remove</span>
    </p>
    <p>
      <span class="add">Add fields</span>
    </p>

  </form>


  <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

  <script type="text/javascript">
    $(".add").click(function() {
      $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
      return false;
    });

    $(".remove").click(function() {
      $(this).parent().remove();
    });

</script>

</body>
</html>

Copyright Notice:Content Author:「Nelson Kshetrimayum」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/67157188/django-dynamic-jquery-form-not-working-when-csrf-token-is-added

More about “Django: Dynamic jquery form not working when {% csrf_token %} is added” related questions

Django: Dynamic jquery form not working when {% csrf_token %} is added

I am using a dynamic form in my django template using jquery but when I add submit button or csrf token to the form the dynamic form stops working and I can not add new fields anymore. Here is my h...

Show Detail

How to properly append django csrf_token to form in inline javascript?

I have created form element dynamically in javascript: var selectform = document.createElement("form") I have added other input elements and these attributes to selectform: submitnselection.

Show Detail

Django Form csrf_token

whenever we want to perform a POST request in django we need to add a csrf_token. For example if you want to create a form: &lt;form action="#" method="POST"&gt; {% csrf_token %} This is pretty s...

Show Detail

Firefox not receiving django csrf_token

I am submitting a ajax form in django and using xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); to get csrf_token. The form is working well in chrome. But in firefox the value of

Show Detail

using csrf_token in django widget

I am trying to make a custom django widget with ajax. The ajax call requires csrf_token. I need to know how to pass the csrf_token to the widget html. # forms.py from django import forms from widg...

Show Detail

Submit Django search form and use .jquery .load for the results?

I'm building a search function with Jquery and Django. And Im wondering how I combine the jquery .load function with a form submit. This is what im working with. I click a link which open a dialo...

Show Detail

Django CSRF Issues While Using Dynamic Formsets and Django Formtools

I am using Django formttools to create a multistep wizard. In one of the forms I have a question option where users need to add as many question as may be need. I have achieved the dynamic formsets

Show Detail

Django-Dynamic Form Increment ID for each new input element added in formset

I have a form which takes 4 fields as input as below:- forms.py AuthorFormset = modelformset_factory( Author, fields=('due_date','author' ), extra=5, widgets={'due_date': form

Show Detail

How to add csrf_token to manually created HTML form?

When creating a HTML form using Django format_html, I need to insert the csrf_token at the place of {% csrf_token %} below, since the use of {% csrf_token %} of course don't substitute when using

Show Detail

jQuery widgets not working for dynamically added formset forms

I have a form and a related formset in my view/template. For several fields I use widgets: Select2 and a calendar (date-picker) from django admin app. I also use dynamic addition of formset forms....

Show Detail