Sequery
Sequery

Reputation: 90

'SafeString' object has no attribute 'get'

I want to render string to a template and I think I misunderstand something. Here is my code :

        context = { 'order': order,
                    'ordered_items': ordered_items,
                    'change': change,
                    }
        return render_to_string('order/receipt.html', context)

And I use them like this:

<div id="capture">
            <div class="print_container">
                <h3>Market Name</h3>
                <span>---------------------------------------</span>
                <br>
                <span>Order number:
                    {{ order.code }}
                </span>
                <br>
                <br>
                <span>Order time:
                    {{ order.created }}</span>
                <br>
                <br>
                <span>Cashier:
                    {{ order.waiter }}</span>
                <br>

                <span>---------------------------------------</span>
                <div class="section4">
                    <div>
                        <table>
                            <thead>
                                <tr>
                                    <th width="110">Product name</th>
                                    <th width="80">Unit price</th>
                                    <th width="30">Quantity</th>

                                </tr>
                            </thead>
                            <tbody>
                                {% for item in ordered_items %}
                                    <tr>
                                        <td>{{ item.food.title.tm }}</td>
                                        <td>{{ item.food_size.price }}</td>
                                        <td>{{ item.quantity }}</td>
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    <br>
                    <span>---------------------------------------</span>
                    <br>
                    <div class="total">
                        <span>Total :
                            {{ order.total_cost }}</span>
                        <br><br>
                        <span>Paid :
                            {{ order.paid }}
                        </span>
                        <br><br>
                        <span>Change :
                            {{ change }}
                        </span>
                    </div>
                    <br>
                    <span>---------------------------------------</span>
                </div>
                <div class="section5">
                    <span>Thank you for your patronage!
                    </span>
                </div>
            </div>
        </div>

I'm going to use this html template to print a receipt via thermal printer. And also I want to know more about render_to_string method, I read the docs but can't understand well.

Upvotes: 1

Views: 2960

Answers (1)

taha maatof
taha maatof

Reputation: 2165

   def your_function(request):
     ......# your queries 
     context = { 'order': order,'ordered_items': ordered_items,'change': change}
     html = render_to_string(order/receipt.html', context)  
     # add here the logic you want 
     return redirect('some_url/') # or just return html, depends of what you want to do

Update

def admin_order_pdf(request, id):
product = get_object_or_404(Product, id=id)
html = render_to_string('orders/order/pdf.html',
                        {'product': product})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
weasyprint.HTML(string=html).write_pdf(response,
    stylesheets=[weasyprint.CSS(
        settings.STATIC_ROOT + 'css/pdf.css')])
return response

it's just an example you could adjust to your project

Upvotes: 1

Related Questions