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
-
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– akirashimosoeda2019-11-25 12:17:27 +00:00Commented Nov 25, 2019 at 12:17
-
Yes. I think @akirashimosoeda already answered. You may want to use Django REST framework to build the API.HiroshiFuu– HiroshiFuu2019-11-25 12:20:01 +00:00Commented Nov 25, 2019 at 12:20
-
@ak,@Feng Hao can you people suggest any example urls for this problem as you said..gig– gig2019-11-25 12:25:25 +00:00Commented Nov 25, 2019 at 12:25
-
Can you show the model which will be used to store info?Amandeep Singh– Amandeep Singh2019-11-25 12:41:42 +00:00Commented Nov 25, 2019 at 12:41
2 Answers
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>
7 Comments
var data variable by reading from either context variables or wherever. This is an example for a post request delete operation.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