Open dkirkham opened 1 year ago
We could check for request in the template tag, wouldn't hurt
It's tempting to put the tags from this package in a base template but there are templates where that doesn't make sense (404, 500, csrf error etc). Schema tags are usually used for SEO and these pages aren't typically indexed
I'd recommend having a intermediary template that your pages inherit from that includes the schema tags that inherits your base template so the structure looks something like
base.html -> page_base.html (ld tags here) -> page_1.html, page_2.html etc
\
-> 404.html, 403_csrf.html etc
Yes, makes sense.
I found another templatetag with the same problem, and I had a deadline, so I've hardcoded the 403_csrf.html for now. I'll apply your solution when I get a chance.
Under some circumstances - such as CSRF errors - django core will attempt to render the
403_csrf.html
template, and thecontext
will not include therequest
key. If the template includeswagtailschemaorg_tags
and in particularld_for_site
the template tag will itself cause aKeyError
exception. This creates a 500 response and spams the error logs:From what I've read, ideally, template tags facing this sort of problem should fail silently.
As the
ld_for_site
template tag is often in abase.html
file included into most page templates and in this case, the403_csrf.html
template, it is a bit messy to work around this issue in the template system. My workaround, until this package is fixed, is to reimplementld_for_site
in my own code, returning an empty string if therequest
key is not present.