fix 404 handling in json ld
This commit is contained in:
parent
4f1bbdd147
commit
b35ce84792
1 changed files with 23 additions and 7 deletions
|
@ -1,6 +1,6 @@
|
||||||
# hub/services/templatetags/json_ld_tags.py
|
# hub/services/templatetags/json_ld_tags.py
|
||||||
from django import template
|
from django import template
|
||||||
from django.urls import resolve
|
from django.urls import resolve, Resolver404
|
||||||
from django.utils.safestring import mark_safe
|
from django.utils.safestring import mark_safe
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@ -13,13 +13,12 @@ def json_ld_structured_data(context):
|
||||||
Generates appropriate JSON-LD structured data based on the current page.
|
Generates appropriate JSON-LD structured data based on the current page.
|
||||||
For schemas, see https://schema.org/
|
For schemas, see https://schema.org/
|
||||||
"""
|
"""
|
||||||
request = context["request"]
|
request = context.get("request")
|
||||||
current_url = request.path
|
if not request:
|
||||||
resolved_view = resolve(current_url)
|
return ""
|
||||||
view_name = resolved_view.url_name
|
|
||||||
|
|
||||||
# Base URL for building absolute URLs
|
current_url = request.path
|
||||||
base_url = request.build_absolute_uri("/").rstrip("/")
|
base_url = f"{request.scheme}://{request.get_host()}"
|
||||||
|
|
||||||
# Default organization data (for Servala)
|
# Default organization data (for Servala)
|
||||||
organization_data = {
|
organization_data = {
|
||||||
|
@ -43,6 +42,23 @@ def json_ld_structured_data(context):
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Add try-except around the view resolution
|
||||||
|
try:
|
||||||
|
resolved_view = resolve(current_url)
|
||||||
|
view_name = resolved_view.url_name
|
||||||
|
except Resolver404:
|
||||||
|
# For 404 pages or other unresolvable URLs, return minimal data
|
||||||
|
return mark_safe(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"@context": "https://schema.org",
|
||||||
|
"@type": "WebSite",
|
||||||
|
"name": "Servala - Open Cloud Native Service Hub",
|
||||||
|
"url": base_url,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Handle different page types
|
# Handle different page types
|
||||||
if view_name == "homepage":
|
if view_name == "homepage":
|
||||||
data = [
|
data = [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue