How to do it...

Execute the following steps one by one:

  1. In the likes app, create a templatetags directory with an empty __init__.py file to make it a Python module. Then, add the likes_tags.py file, where we'll define the {% like_widget %} template tag as follows:
# myproject/apps/likes/templatetags/likes_tags.pyfrom django import templatefrom django.contrib.contenttypes.models import ContentTypefrom django.template.loader import render_to_stringfrom ..models import Likeregister = template.Library()# TAGSclass ObjectLikeWidget(template.Node):    def __init__(self, var):        self.var = var    def render(self, context):        liked_object = self.var.resolve(context)        ct = ContentType.objects.get_for_model(liked_object)        user = context["request"].user        if not ...

Get Django 3 Web Development Cookbook - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.