Print Format address not updating

I have a custom print format for Payment Entry with the customer address displayed in the header. The code for this is:

<div class="print-heading" style="clear: both;">
<span style="float: left; text-align: left">
{% set ad = frappe.get_doc("Address", doc.customer_address) %}
{{ ad.address_line1 or '' }}<br>
{{ ad.address_line2 or '' }}<br>
{{ ad.city or '' }}<br>
{{ ad.pincode or '' }}<br>
{{ ad.country or '' }}
</span>
<h2>Remittance<br><small>{{ doc.party_name }}</small></h2>
</div>

However, it will not update the address for different customers. I am sure that I have different addresses for my different payees, and know that some have no address at all. Clearing cache has no effect.

How can I force Jinja to update the template using the correct data every time?

I don’t know where it got doc.customer_address from initially, but I couldn’t replicate the behaviour. Rewriting the whole thing using frappe methods (and appropriate conditionals in case there is no address specified) gives the following:

<div class="print-heading" style="clear: both;">
<span style="float: left; text-align: left">
{% if frappe.db.get_value("Dynamic Link", {'link_name': doc.party_name}, 'parent') %}
{% set dl = frappe.db.get_value("Dynamic Link", {'link_name': doc.party_name}, 'parent') %}
{% set ad = frappe.get_doc("Address", dl) %}
{% if ad.address_line1 %}
{{ ad.address_line1 }}<br>
{% endif %}
{% if ad.address_line2 %}
{{ ad.address_line2 }}<br>
{% endif %}
{% if ad.city %}
{{ ad.city }}<br>
{% endif %}
{% if ad.pincode %}
{{ ad.pincode }}<br>
{% endif %}
{% if ad.country %}
{{ ad.country }}
{% endif %}
{% endif %}
</span>
<h2>Remittance<br><small>{{ doc.party_name }}</small></h2>
</div>

This seems to always update the address correctly when the template is loaded.