A
A
ArrTem7772022-03-21 12:20:08
Python
ArrTem777, 2022-03-21 12:20:08

How to implement likes in python with minimal js?

How to implement likes (backend in python, Flask) with minimal use of js?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2022-03-21
@ArrTem777

It is not clear what likes are, well, let it be a rating system for any post. Create two plus and minus buttons. According to the smart, probably, it will be necessary to somehow make it into the form, and generally use the form flask, but let it be just like that. Connect jQuery to the page. Write a simple click handler for these buttons, so that when clicked, it sends a request to some of your endpoints, taking into account the votes for the post.

<body>
   
   <span class="vote-button" data-action="upvote">+</span>
   <span class="vote-button" data-action="downvote">-</span>


   <script>
   $(document).ready(function () {
    $(".vote-button").click(function () {
        var action = $(this).data("action");

        $.ajax({
            type: "POST",
            url: "api/vote",
            data: {
                action: action,
                postid: postid
            }
        });
    });
});
   </script>
</body>

In the flask, you already accept this request, get the action and postid from it, and make the appropriate changes to the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question