Generating Django model entities based on HTML input

Within my Django project, there is a model named Exercise.
On the HTML page, there is a table displaying all exercises associated with the user.

In the snippet below, you can view the code for the table showcasing the user's exercises, along with buttons to append new rows.

Purpose: Upon adding a new row to the table, I aim to generate a blank instance of the Exercise model as well.

If such functionality isn't possible through basic Django and JQuery, kindly suggest frameworks that support it. Many thanks.

$('.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 %}

Answer №1

Implementing this functionality in Django with Ajax using either Jquery or Javascript is entirely feasible. By triggering a "post" request from your add button, you can easily create an instance of your model. It's essential to handle the CSRF token properly. Here is a reference to the Django Documentation for Ajax that provides more insight.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering a specific property of an object within an array using Typescript

My task involves retrieving an employer's ID based on their name from a list of employers. The function below is used to fetch the list of employers from another API. getEmployers(): void { this.employersService.getEmployers().subscribe((employer ...

Sharing $scope Data Between Controller and Directive in AngularJS

Recently, I created a straightforward directive that displays an object: var app = angular.module("myApp", []); app.controller('myCtrl', function($scope) { $scope.users=[ {name:'davidi',age:'23'}, {name:'karen ...

Examining the performance of a React application through the use of Google Chrome

Currently, while working on my react application, I have been utilizing the chrome developer tool to monitor performance. However, I am having difficulty identifying which specific function is causing a high level of computational intensity. Could anyone ...

How to obtain the current URL of an iframe?

After coming across what seems to be a similar question, I still have to ask for clarification because I couldn't find a clear answer. Allow me to elaborate on my project, I am working on an online store where we offer two types of products, The fi ...

Understanding the "this" keyword in React.js is crucial for

Looking for a solution: updateSongIndex: function(index) { this.setState({currentSongIndex: index }); } I want to trigger the updateSongIndex function by clicking on a div element and passing an index as an argument var trackList = d ...

What is the best way to ensure that child components are "ready" before proceeding with React hooks?

I have a component called Page, which contains multiple instances of the Column component, each with its own user-specific markup. Within the Page component, I need to perform additional actions (such as checking for column overflow and adjusting content ...

Sophisticated method for implementing dynamic components with props in Vue

In a specific scenario, I am using an API to output all the content for my Page, including its structure. To provide context, imagine a CMS with a page builder feature where authors can place components via drag and drop to create pages, which are then del ...

How can one effectively manage asynchronous behavior on the client side of a Meteor application?

My focus is on Meteor. I want to connect with the Facebook API through Meteor's HTTP in order to show images on Meteor's client side. I've come across suggestions like Fiber Futures, storing data in Sessions, and triggering a synchronous se ...

What is the best way to retrieve JavaScript Values through Selenium?

Currently, I am working with Java selenium client and running this code snippet. The variable PAGE_NUMBER is assigned a value; however, when using selenium, I'm unable to retrieve it: String script = "var cellValue = selenium.browserbot.getUserWindow ...

Build a React application using ES6 syntax to make local API requests

I'm really struggling to solve this problem, it seems like it should be simple but I just can't figure it out. My ES6 app created with create-react-app is set up with all the templates and layouts, but when trying to fetch data from an API to in ...

Is there a way to output a specific attribute of an object obtained from a CSV file?

I successfully imported a csv file into my JavaScript code and now I am looking to extract and print specific data related to the batsman in the console. Specifically, I want to display the batsman's runs and name from the objects extracted from the ...

Personalize the marker design within the Wikitude SDK using JavaScript

Currently, I am developing an augmented reality view in Android using the wikitude-sdk. As part of the project, I am displaying markers on the screen and now I am looking to customize the marker view using the AR.HtmlDrawable method offered by the ...

Grafana: Setting up InfluxDB queries using panel plugin. Issue with HTML input element after value is set by TypeScript

I have made modifications to an existing Grafana panel plugin called Boom table, allowing it to read a configuration file and update patterns and thresholds with the data from that file. Now, I want to further update the Data Source queries and aliases to ...

I am having issues with my CSS file not functioning correctly on my Django template. Does anyone have any suggestions on how to resolve this issue

I've encountered issues with CSS files not working properly on my Django template. Whenever I make changes to the CSS file and reload the browser page, nothing happens. However, if I restart my computer or power off and on again, the changes reflect o ...

struggling to update an array with user inputs on my to-do list app

I'm currently in the process of creating a small to-do list with some specific functionality. I want each text input (or a copy of it) to be added to an empty string, ensuring that the original input remains unchanged. The goal is to have a function t ...

Tips on arranging my Bachelor's in Computer Science degree certificate and academic awards at the bottom

I am trying to make the BS-CS and stars section stick to the bottom of my container, but I am facing difficulties in achieving that. Can someone help me with this? Here is my Bootstrap code: <div className="item1 col-12 col-md-6 col-lg-4 col-xl-4 my- ...

Exploring the Integration of Medium Widgets within a React Website

I stumbled upon an incredible link here that generates widgets for Medium posts. Unfortunately, I encountered difficulties when trying to incorporate the code into a react website. For example, for a random Medium author: <div id="medium-widget">& ...

Using TypeScript to determine the week number - the value on the right side of the math operation must be of data type 'any'

I've spent a lot of time searching for code to help me calculate the week number in my Angular app according to ISO standards. It's been challenging to find JavaScript-specific code, but I believe I may have found something - however, I encounter ...

Prevent Html.Action function from being invoked during page loading

Below is a div within a parent view that contains a Html.Action returning a PartialView: <div id="preview" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> ...

Increasing a value within HTML using TypeScript in Angular

I'm working with Angular and I have a TypeScript variable initialized to 0. However, when trying to increment it using *ngFor in my .ts file, the increment is not happening (even though the loop is running correctly). my-page.html <div *ngFor=&quo ...