Skip to content

Commit

Permalink
Fix base_url template tag
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed May 23, 2024
1 parent 9591f71 commit 3f69047
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions adit_radis_shared/common/templatetags/common_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from datetime import date, datetime, time
from typing import Any

from django.conf import settings
from django.contrib.sites.models import Site
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpRequest
from django.template import Library
Expand All @@ -19,10 +21,17 @@ def access_item(d: dict, key: str) -> Any:

@register.simple_tag(takes_context=True)
def base_url(context: dict[str, Any]) -> str:
# Requires django.template.context_processors.request
request: HttpRequest = context["request"]
domain = get_current_site(request).domain
protocol = "https" if request.is_secure() else "http"
# To have request in context, django.template.context_processors.request
# is required in the context_processors settings.
request: HttpRequest | None = context.get("request")

if request is not None:
domain = get_current_site(request).domain
protocol = "https" if request.is_secure() else "http"
else:
domain = Site.objects.get_current()
protocol = "https" if not settings.DEBUG else "http"

return f"{protocol}://{domain}"


Expand Down

0 comments on commit 3f69047

Please sign in to comment.