0

The task I want to accomplish is simple: track the number of times a post on my blog has been shared because I want the number to be displayed beside the post. However, I cannot figure it out.

Here is the the anchors with hrefs that allow the user to click on them and share the post on social media:

<ul class="mb-30 list-a-bg-grey list-a-hw-radial-35 list-a-hvr-primary list-li-ml-5">
    <li class="mr-10 ml-0">Share</li>
    <li><a href="https://www.facebook.com/sharer/sharer.php?u={{ request.build_absolute_uri }}"><i class="ion-social-facebook"></i></a></li>
    <li><a href="http://twitter.com/share?url={{ request.build_absolute_uri }}"><i class="ion-social-twitter"></i></a></li>
</ul>

I have a Post model with a variable called share_count. Obviously, I have a view that helps renders out the HTML too. My issue is that I do not understand how to connect the hrefs to my view in order to increment the share_count of the post in the database.

I searched all over and could not find an answer, I would appreciate any help or hints.

Also, can you explain it as if you are telling someone who just started web development...oh wait, that is exactly what is going on!

  • 2
    You can query the APIs of Facebook/Twitter to obtain the share count: https://stackoverflow.com/a/19119580/67579 – Willem Van Onsem Aug 22 '19 at 14:42
  • Alternatively, you can intercept the click event in javascript (make sure your `preventDefault()`), send an ajax request to a view that collects the shares, and finally in javascript set the window location to the url of the button (twitter or facebook) so the user loads the share page. – dirkgroten Aug 22 '19 at 14:50
  • 1
    What if all I want to do is increment a counter in my database everytime one of the anchors is clicked? How would I do that? @WillemVanOnsem –  Aug 23 '19 at 14:19

0 Answers0