#36244: safestring
-----------------------------+-----------------------------------------
Reporter: WeeDom | Type: Bug
Status: new | Component: Uncategorized
Version: 5.1 | Severity: Normal
Keywords: safestring. | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-----------------------------+-----------------------------------------
Trying to render html with some basic inputs from a form. Testing with
`manage.py test order` (order being my app, for the avoidance of doubt)
order_email_to_vendor.html:
{{{
<!DOCTYPE html>
<html>
<head>
<title>New Order</title>
</head>
<body>
<p>Hi, AK,</p>
<p>An order has been placed by {{ name }}. Details below:</p>
<ul>
<li>Name: {{ name }}</li>
<li>Phone: {{ phone }}</li>
<li>Email: {{ email }}</li>
<li>Address: {{ address1 }}</li>
<li>Comments: {{ comments }}</li>
</ul>
<p>Cheers,<br>Your friendly robot helper.</p>
</body>
</html>
}}}
my rendering code is this:
{{{
context = {
'name': form.cleaned_data['name'],
'phone': form.cleaned_data['phone'],
'email': form.cleaned_data['email'],
'address1': form.cleaned_data['address1'],
'comments': form.cleaned_data['comments'],
}
# Add this line to inspect the context data
vendor_email_html =
render_to_string('order/order_email_to_vendor.html', context) + " testing
by Dom"
vendor_email_plain = strip_tags(vendor_email_html)
customer_email_html =
render_to_string('order/order_email_to_customer.html', context)
customer_email_plain = strip_tags(customer_email_html)
send_mail(
'New Order',
vendor_email_plain,
settings.DEFAULT_FROM_EMAIL,
[settings.CLIENT_EMAIL],
fail_silently=False,
html_message=vendor_email_html,
)
}}}
email sends ok, but with an empty body.
I set a trace in my code. After a LOT of s/n, I came across this. (note -
`context` is generated by `manage.py test`, not me. And, anyway, `context`
isn't the problem)
{{{
> /home/weedom/ak-cakes/venv/lib/python3.12/site-
packages/django/template/base.py(1008)render()
-> return SafeString("".join([node.render_annotated(context) for node in
self]))
(Pdb) context
[{'True': True, 'False': False, 'None': None}, {'name': 'John Doe',
'phone': '123456789', 'email': '
jo...@example.com', 'address1': '123
Street', 'comments': 'No nuts, please'}]
(Pdb) self
[]
(Pdb) SafeString("".join([node.render_annotated(context) for node in
self]))
'' # empty string - template has disappeared.
(Pdb) s
--Return--
> /home/weedom/ak-cakes/venv/lib/python3.12/site-
packages/django/template/base.py(1008)render()->''
-> return SafeString("".join([node.render_annotated(context) for node in
self]))
(Pdb) s
}}}
when it runs the template through SafeString, it returns an empty string.
No error/exception.
I've managed to work around this with `{% autoescape off %} ` but that
feels *really* icky.
Reporting as a bug because some sort of hint as to what happened would
have saved me hours.
Thanks
WeeDom
--
Ticket URL: <
https://code.djangoproject.com/ticket/36244>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.