Jinja2邮件模板渲染问题:表达式因无值无法求值求助
Let's break down your problem and walk through possible fixes—this error usually stems from either template syntax missteps or out-of-order processing between your SQL placeholder replacement and Jinja2 rendering.
First: Fix Your Jinja Variable Definitions
Looking at your code, you’ve wrapped your {% set %} statements inside HTML comments:
<!-- {% set partner_shortname = '@@SHORTNAME' %} --> <!-- {% set partner_name = '@@PARTNER' %} -->
Jinja2 ignores code inside HTML comments—those variable assignments aren’t being executed at all. That means partner_name and partner_shortname never get defined in your template context, which directly causes the "Expression not evaluating due to no value" error when you try to render {{ partner_name }}.
Fix: Remove the HTML comment tags so Jinja can process the assignments:
{% set partner_shortname = '@@SHORTNAME' %} {% set partner_name = '@@PARTNER' %}
Second: Check the Order of SQL Replacement & Jinja Rendering
You noted that SQL should assign values to @@-formatted variables—your workflow order here is critical.
If your current process is:
- Jinja2 renders the template first
- Then SQL replaces the
@@placeholders
That’s backwards. By the time Jinja tries to evaluate {{ partner_name }}, the value is still the raw string '@@PARTNER'—which your system likely doesn’t recognize as a valid value, triggering the error.
Fix: Rearrange your workflow to run SQL placeholder replacement before passing the content to Jinja2. Let SQL replace @@SHORTNAME and @@PARTNER with their actual values first, so Jinja receives a template with real, usable data.
Third: Validate SQL Placeholder Replacement Logic
Even with the right order, double-check that your SQL replacement is working as intended:
- Are placeholder names (
SHORTNAME,PARTNER) spelled identically in both the template and your SQL logic? - Does your SQL have valid values assigned to these placeholders? A missing assignment will leave
@@PARTNERuntouched, leading to the same error. - Is the replacement case-sensitive? For example, if SQL looks for
@@partnerbut your template uses@@PARTNER, it won’t replace anything.
Quick Safety Net: Add Default Values
To avoid this error even if variables are missing or unprocessed, use Jinja2’s default filter to fall back to a friendly placeholder:
<p>PARTNER NAME: {{ partner_name | default('No Partner Specified') }}</p>
This will render the fallback text instead of throwing an error if partner_name is undefined or empty.
内容的提问来源于stack exchange,提问作者FROSTYSMOOTH

