Wagtail default template field in_preview_panel is fine in dev but fails in production
In the default template from Wagtail even mentioned online here, there is a part:
{# Force all links in the live preview panel to be opened in a new tab #}
{% if request.in_preview_panel %}
<base target="_blank">
{% endif %}
I extend this base template in my other templates.
This is fine in dev, but when running the website in production, I face with this error:
Exception while resolving variable 'in_preview_panel' in template 'home/home_page.html'.
Traceback (most recent call last):
File "/home/myusername/.local/lib/python3.12/site-packages/django/template/base.py", line 880, in _resolve_lookup
current = current[bit]
~~~~~~~^^^^^
TypeError: 'WSGIRequest' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/myusername/.local/lib/python3.12/site-packages/django/template/base.py", line 890, in _resolve_lookup
current = getattr(current, bit)
^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WSGIRequest' object has no attribute 'in_preview_panel'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/myusername/.local/lib/python3.12/site-packages/django/template/base.py", line 896, in _resolve_lookup
current = current[int(bit)]
^^^^^^^^
ValueError: invalid literal for int() with base 10: 'in_preview_panel'
During handling of the above exception, another exception occurred:
...
I have no clue on what in_preview_panel
is and why it behaves different in dev and prod.
If I remove it, this case of error will be resolved. I have still other problems in my code. But, I do not want to remove this blindly or impacting the Wagtail admin. Instead, I want to understand what is happening.
request.in_preview_panel
is set to True when the page is being viewed inside the preview panel in the Wagtail admin, and undefined otherwise. The code snippet here is making use of this to insert the <base target="_blank">
tag (to open links in a new window) when the page is being previewed.
In Django template code, undefined variables are normally silently ignored, so when viewing the page for real (not in a preview) the {% if request.in_preview_panel %}
should be skipped over. Apparently this is happening correctly on your dev site, but not in production.
Firstly - is the page actually failing to display, or are you just seeing the error messages in the logs while the page displays normally? If it's the latter, there's nothing to worry about - that just means you have your logging set to DEBUG level. To make it less verbose, you can change this to WARNING or ERROR in your project settings.
If this is causing an outright failure, we really need to see the full error traceback to understand what's going on, rather than just the section you've quoted here. I suspect there may be something non-standard in your project settings, like a string_if_invalid
setting - I suggest carefully comparing your project settings between your development and production sites.