I am having the cards in the 4 pages and I am having a pagination too I want for every click of card by the user the card content should store in the database using django. For storing the form data in database using django we can do using csrf_token and post method but can you people suggest how to store the cards data in the database on every click by the user?? please refer the link given https://www.modsy.com/project/room and suggest how that cards content will be stored in database on click by the user
Asked
Active
Viewed 136 times
0
-
Maybe using a client side Javascript script might solve your issue. Requesting a http post request whenever you click your card. Finally, you need to implement the endpoint in django. – akirashimosoeda Nov 25 '19 at 12:17
-
Yes. I think @akirashimosoeda already answered. You may want to use Django REST framework to build the API. – HiroshiFuu Nov 25 '19 at 12:20
-
@ak,@Feng Hao can you people suggest any example urls for this problem as you said.. – gig Nov 25 '19 at 12:25
-
Can you show the model which will be used to store info? – Amandeep Singh Nov 25 '19 at 12:41
2 Answers
1
Here's a working example, suppose there's a delete anchor tag in your template.html
<a onclick="delOnClick(this)" id="{{ task.id }}"> Delete </a>
and in your <scripts></scripts>
tag on html
template, you add this ajax
<script>
function delOnClick(ref) {
var url = "{% url 'app:model_delete' %}"
var id = $(ref).attr("id")
var intId = parseInt(id)
var data = { id: intId, csrfmiddlewaretoken: '{{ csrf_token }}', contentType: 'application/json' }
$.post(url, data, function (data, status) {
location.reload(true)
})
}
</script>
Now what this is doing is calling the delete view which supposedly receives a post request which id
of element to delete in it's body.
If your delete view is a get view, it's even simpler, you just have to add id
in url and call get
request instead of post
request.
Make sure to add ajax
in your page.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

saadi
- 646
- 6
- 29
-
Now you can add whatever data you want to your `var data` variable by reading from either context variables or wherever. This is an example for a `post` request delete operation. – saadi Nov 25 '19 at 13:12
-
Here in the link I have given above I had used pagination for next and back like that I am having 4 pages after clicking 4 times next the data should store in database using post request can you please explain it how??@saadi. – gig Nov 26 '19 at 02:54
-
So you mean it works like a wizard? You want to keep data from each step and then after the final step, dump this in database by calling some view. – saadi Nov 26 '19 at 07:09
-
-
I don't have a shareable example with me right now, but [here](https://swapps.com/blog/how-to-do-a-wizard-form/) it is done with an example. Might be what you are looking for. – saadi Nov 26 '19 at 11:42
-
-
is the link provided by you the wizardsessionview works only for forms? I need for templates i.e the individual html pages is there anything related to my question please share..@saadi – gig Nov 26 '19 at 11:55
0
You should use Javascript and AJAX, using django rest framework.
Your Javascript on your html template will call an APIView, created with django REST.
example here :
How to make a POST request using DJANGO REST framework with ajax

Epikstar
- 69
- 5
-
Thankyou for your suggestion @Epikstar can you people suggest any example resources on how to do it. – gig Nov 25 '19 at 12:28
-