pythondjangoformsetdjango-formtools

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 through Django Dynamic Formset but although I have the {% csrf_token %} in my template I still get CSRF issues. Here is my template:

 {% block content %}
  <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
  <form action="" method="post" enctype="multipart/form-data" id="job-question">{% csrf_token %}
    <table>
    {{ wizard.management_form }}
{% if wizard.form.forms %}
    {{ wizard.form.management_form }}
    {% for form in wizard.form.forms %}
        {{ form }}
    {% endfor %}
{% else %}
    {{ wizard.form }}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>


</form>

<script type="text/javascript" src="{% static 'js/jquery-min.3.4.1..js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery.formset.js' %}"></script>
<script type="text/javascript">
    $(function() {
        $('#job-question').formset();
    })
</script>
{% endblock %}

Solution

  • I managed to handle this by passing each form with its id to the dynamic formset JS function. That way validation was done wholly.