lalin
lalin

Reputation: 11

'Template' object has no attribute 'replace'

Whenever I run the code, the error is this:

'Template' object has no attribute 'replace'

Any help would be nice. Thanks!

Upvotes: 1

Views: 1297

Answers (1)

Chris
Chris

Reputation: 2212

The line

template = get_template("user/ticket_print.html")

returns a Template object and not a string containing the code you put in your template (which I think you expect). And the Template object does not have a method called replace() resulting in your error.

To access the string you can use

template.template.source

So I think in your case a slight change in your view could do the trick:

def ticket_print(request, cartitem_id):
    item = get_object_or_404(CartItem, object_id=cartitem_id)
    template = get_template("user/ticket_print.html")
    value = template.template.source
    html_result = render_template(value, {"itest": item.cart,},)

   return html_to_pdf(html_result, name=f"Ticket_Print{item}")

Upvotes: 2

Related Questions