0

Scenario

I have an API GET request which filters a DB table for a company called "Cat Inc." and returns info of employees part of "Cat Inc":

[
  {
    "company": "Cat Inc."
  },
[
  {
    "id": 1,
    "name": "Bob"
  },
  {
    "id": 2,
    "name": "Cat"
  }
 ]
]

I have a plain-text Jinja2 template company.j2:

The company name is {{ company }}.

{% for employee in employees %}
  {{ employee.name }} is part of the company with ID of {{ employee.id }}
{% endfor %}

And would ultimately like to return this:

The company name is Cat Inc.

Bob is part of the company with ID of 1 
Cat is part of the company with ID of 2

I've set the API @get request to return company info and employee info separately as list type variables, and following the docs, I tried something like this:

@get("/{company_id}", response_class=PlainTextResponse)
async def generate_template(company_id: int, request: Request):

     company = *<uses some function to filter to find Cat Inc.'s company_id>*

     employees = *<uses some function to find employees where company_id = Cat Inc.company_id>*

return templates.TemplateResponse("company.j2", {"request": Request, 
                                  "company": company.company, 
                                  "employees": employees.employee})

What I seem to return in the API request is:

The company name is Cat Inc.

And that is it. The for loop within the Jinja2 template gets ignored.

Chris
  • 18,724
  • 6
  • 46
  • 80
  • Does this answer your question? [How to navigate through FastAPI routes by clicking on HTML button in Jinja2 Templates?](https://stackoverflow.com/questions/74237542/how-to-navigate-through-fastapi-routes-by-clicking-on-html-button-in-jinja2-temp) – Chris Dec 19 '22 at 15:45
  • Please have a look at related answers [here](https://stackoverflow.com/a/71121812/17865804), as well as [here](https://stackoverflow.com/a/70996841/17865804) and [here](https://stackoverflow.com/a/74584131/17865804). – Chris Dec 19 '22 at 15:47
  • No luck! the "Request" part was a typo. 'request': request still yields nothing. – Wonseok Choi Dec 20 '22 at 00:15
  • @Chris Thanks! Managed to solve it after dwadling with the links you provided. Thank you! – Wonseok Choi Dec 20 '22 at 03:25

1 Answers1

1

Credit to Chris in the comments section of the question above:

Changing the API response context for the template to take the entire dict instead of trying to specify the k/v pair of the dict that was required itself:

@get("/{company_id}", response_class=PlainTextResponse)
async def generate_template(company_id: int, request: Request):

     company = # *<uses some function to filter to find Cat Inc.'s company_id>*

     employees = # *<uses some function to find employees where company_id = Cat Inc.company_id>*

     return templates.TemplateResponse("company.j2", {"request": request, 
                                  "company": company.company, 
                                  "employees": employees})

And I was able to iterate through the list no problem.

Chris
  • 18,724
  • 6
  • 46
  • 80