0

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

4
  • 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. Commented Nov 25, 2019 at 12:17
  • Yes. I think @akirashimosoeda already answered. You may want to use Django REST framework to build the API. Commented Nov 25, 2019 at 12:20
  • @ak,@Feng Hao can you people suggest any example urls for this problem as you said.. Commented Nov 25, 2019 at 12:25
  • Can you show the model which will be used to store info? Commented Nov 25, 2019 at 12:41

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

7 Comments

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.
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.
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.
Yes @saadi you are right? can you say how to do this with an example?
I don't have a shareable example with me right now, but here it is done with an example. Might be what you are looking for.
|
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

2 Comments

Thankyou for your suggestion @Epikstar can you people suggest any example resources on how to do it.
i just shared an example in my comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.