0

In Django, I have a model called Exercise.
Then, on an HTML page, I have a table with all of the exercises that belong to that user.

In the following (shortened) code, you can see that I have a table that displays all of the exercises that the user currently has. Also, it has buttons to add new rows to the table.

Goal: when I add a new row to a table, I want to also create a blank instance of the Exercise model.
br>

Also, if that is not available in just simple Django and JQuery, please let me know which frameworks support it. Thank you.

$('.deleteButton').on('click', function () {
    if ($(this.parentNode.parentNode.parentNode).find('tr').length > 3) {
        $(this.parentNode.parentNode.parentNode).find("tr:nth-last-child(2)").remove();
    }
})
$('.addButton').click(function () {
    $(this.parentNode.parentNode.parentNode).find('tr:last').before('<tr><td>name</td><td>sets</td><td>reps</td><td>weight</td></tr>')
})
{% block content %}

    <div class="row" style="margin-top: 1%;">
        {% for workout in workouts %}
        <div class="col-6 col-lg-3" style="margin-right: 50px;">
            <table class="mytable">
                {% for exercise in workout.exercise_set.all %}
                <tr>
                    <td>{{ exercise.name }}</td>
                    <td>{{ exercise.sets }}</td>
                    <td>{{ exercise.repetitions }}</td>
                    <td>{{ exercise.weight }}</td>
                </tr>
                {% endfor %}
                <tr id="editrow">
                    <td colspan="2">
                        <input class="addButton" type="button" value="Delete" />
                    </td>
                    <td colspan="2">
                        <input class="deleteButton" type="button" value="Delete" />
                    </td>
                </tr>
            </table>
        </div>
        {% endfor %}
    </div>

{% endblock %}
RuslanZab
  • 115
  • 2
  • 10
  • see [this](https://stackoverflow.com/questions/58645505/how-to-render-a-field-request-without-refreshing-the-page/58645877#58645877) then see [this](https://stackoverflow.com/questions/58646656/ajax-triggered-request-doesnt-update-django-view/58646916#58646916) – Ahmed I. Elsayed Apr 15 '20 at 02:16

1 Answers1

0

This is possible in Django via Ajax(Jquery or Javascript). You can send "post" request from your add button for creating an instance of your model. you will have to take care of CSRF token, following is the link to Django Documentation for Ajax

Neeraj
  • 783
  • 5
  • 8