Opened 3 hours ago

#36244 new Bug

safestring

Reported by: WeeDom Owned by:
Component: Uncategorized Version: 5.1
Severity: Normal Keywords: safestring.
Cc: WeeDom Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

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': 'john@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

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top