Get data from table rows and save in database

18 views
Skip to first unread message

Eugene TUYIZERE

unread,
Dec 4, 2021, 5:17:28 AM12/4/21
to django...@googlegroups.com
Dear Colleagues,

I have data in the table. I want to get the rows value and save them in the database. How can I do this with jquery?
The table I have is like this 
image.png
This is what I have in HTML. The total is automatically calculated as I type the amount

<table id="myTable2">
<tr> <th width="300">Description </th>
<th width="400">Amount</th>
</tr>
<tr>
<td><span>School Fess </span></td>
<td><input type="text" class='form-control text-right' /></td>
</tr>
<tr>
<td><span>Restoration </span></td>
<td><input type="text" class='form-control text-right' /></td>
</tr>
<tr>
<td><span>School materials </span></td>
<td><input type="text" class='form-control text-right' /></td>
</tr>

<tr>
<td><span><b>TOTAL </b></span></td>
<td><b><span id="total_sum_value2"></span></b></td>
</tr>
</table>
--
Eugene

Jet Ezra

unread,
Dec 4, 2021, 7:24:57 AM12/4/21
to django...@googlegroups.com
STEPS
1. You will add a name attribute to all your input fields.
2. Add the crsf_token django template in-built filter to support 'POST'
3. Change your form's method to 'POST'
4. Add an action attribute and set it to your view
5. Then the JQUERY part will be as follows
6. Don't forget to give your form ad ID
Then
 $("#form_id").submit(function(e){
e.preventDefault()  //prevent the  default submit behaviour of a form
let data = $(this).serialize() // this picks all data basing on the name attribute including the csrf_token
let URL = $(this).attr('action') //this pick the right URL like http://localhostL:8000/add

//now perform the request
$.ajax({
url:url,
data:data,
type: "POST",
dataType: "json", // or the data type you expect from your response
beforeSend: function(){
// optional-> whatever you want to access before executing the request
// eg showing some loader
},
success: function(response){
// whatever you want to do on successful request
},
complete: () =>{
//whatever you want to do when  the request is complete
// stopping some loader
}

})
})




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABxpZHsPPHbpkNOBDxGofAX-0uHPfGtgXYpHwoQo5iaqGOg9rg%40mail.gmail.com.


--
jet

Lakshyaraj Dash X-D 25

unread,
Dec 4, 2021, 9:02:24 AM12/4/21
to django...@googlegroups.com
Listen you have to target the td inside the tr through a button using js and you can use FormData and fetch api to make a post request for the particular values. Don't forget to implement csrf token like given below before making a post request.

And write these two lines : 
let csrf_token = getCookie('csrftoken')
formData.append('csrfmiddlewaretoken', csrf_token)

** These are required for post request

IMG_20211204_192837152~2.jpg

Fredrick Ekoma

unread,
Dec 4, 2021, 10:25:16 AM12/4/21
to django...@googlegroups.com
what is your product

ram s

unread,
Dec 4, 2021, 10:25:17 AM12/4/21
to django...@googlegroups.com
use name attribute in site input tag...

Reply all
Reply to author
Forward
0 new messages