0

I am developing a website in django and I have encountered a problem.

I am using an 'include' template tag and I am trying to pass an object into the tag. But whenever I pass it in, it is passing in the dunder str method of the object.

{% include 'function/comment' object=answer %}

'answer' is the object I want to pass in (fyi it is a model object)

Instead of passing the answer object it is passing the dunder str method.

Is there a way to solve this?

Thanks in advance.

chai
  • 418
  • 6
  • 17
  • 1
    Does this answer your question? [Django pass object to include](https://stackoverflow.com/questions/7928546/django-pass-object-to-include) – dskalec Jun 28 '21 at 11:53
  • 1
    That is actually what I tried and it still doesn't work – chai Jun 28 '21 at 11:55
  • 1
    Nevermind, I found a workaround and I posted it below – chai Jun 28 '21 at 12:01
  • 1
    Cannot reproduce this (The object is passed normally). Please provide a [mre]. Also you seem to be missing the `with` keyword there i.e. it should be `{% include 'function/comment' with object=answer %}` – Abdul Aziz Barkat Jun 28 '21 at 12:02

1 Answers1

0

Instead of passing the object itself, I passed the id (answer.id) and then I did a query for that id which got the object on the backend side.

chai
  • 418
  • 6
  • 17
  • If you used `answer.id` in the template, then you obviously had the object itself in the template, not just the `str` representation. – dskalec Jun 28 '21 at 12:05