Displaying a list of dictionaries with Jinja2

  1. I’ve got a FastAPI endpoint that’s returning a list of dictionaries:

    @router.get(“/customers/”)
    async def read_customers(*,session: Session = Depends(get_session)):
    customers = session.query(Customers).all()
    return customers

output:

[{"customerName":"Signal Gift Stores","customerNumber":112},...]
  1. How do I modify the endpoint for Jinja2? Everything I’ve tried gives me some kind of an error indicating that a list object can’t be used.

  2. It also appears that I can’t use session in the TemplateResponse context because “context must include a ‘request’ key.”

    @router.get(“/customers/”)
    async def read_customers(*,session: Session = Depends(get_session)):
    customers = session.query(Customers).all()
    return templates.TemplateResponse(“index.html”, What do I do here?)

Any help appreciated.

  • Does this answer your question? You may find this answer helpful as well.

    – 




Leave a Comment