from django import template
from django.conf import settings
from urllib.parse import urljoin

register = template.Library()


@register.simple_tag(takes_context=True)
def canonical_url(context):
    """
    Returns the canonical URL for the current page
    """
    request = context["request"]
    path = request.path

    # Construct the canonical URL using the WEBSITE_URL from settings
    canonical = urljoin(settings.WEBSITE_URL, path)
    return canonical
