CANNOT RENDER HTML IN FASTAPI
See original GitHub issueI am a beginner in python and I am trying to build an app in fastapi.
My task is to get text from html, render html in fastpi run python code on the text and return the output to html page
This is my code
`from typing import Optional
from fastapi import FastAPI, Request, Response from fastapi.staticfiles import StaticFiles from fastapi.responses import HTMLResponse from fastapi.templating import Jinja2Templates,
app = FastAPI()
app.mount(“/static”, StaticFiles(directory=“static”), name=“static”)
templates = Jinja2Templates(directory=“templates”)
@app.get(“/”, response_class=HTMLResponse) async def get(): return templates.TemplateResponse(“layout.html”)
@app.post(“/”) async def post(request: Request): text = request.get(“sent”) `
Issue Analytics
- State:
- Created 3 years ago
- Comments:18 (6 by maintainers)
Top Results From Across the Web
FastAPI - render template in index.html - not working
Good day I am using FastAPI and I want to render the database contents on index.html - however I get the following error:...
Read more >Templates - FastAPI
Use the templates you created to render and return a TemplateResponse , passing the request as one of the key-value pairs in the...
Read more >FastAPI - render template in index.html - not working - Reddit
It seems that you are not passing the notes you're fetching from the database to your Jinja template. Instead, you're trying to pass...
Read more >FastAPI Server HTML response using Jinja2 - Ultra Fast
If you want to render an html resource as the output for any of the routes, its very easy with the help of...
Read more >Jinja2 Templates with FastAPI for Python - YouTube
This video covers how to use Jinja2 templates in a Python FastAPI appThe example code for this project can be found on GitHub...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
WE’D LOVE TO HELP YOU! ☮️
But we need your help first… 🏃
Can you:
Dear @siamhassan66,
I realise you got somewhere, but I can’t find your solution here. I came here to find help and I’m sure more people do the same. So, as promised, here is the solution I came up with, hoping the next person will find it useful.
My approach is to break away from
templates.TemplateResponse
, because I came to realise that this is designed to do just that: pack and send back a response. Instead, I use a two-step approach where I acquire a template — viatemplates.get_template
— and then I render it with some values — viatemplate.render
.This approach allows me to get the generated HTML, while doing away with the need to provide a
request
. This way, the whole processing can be done at middleware level.Here’s the gist:
Please notice that, compared with the previous snippet:
item.html
toitem.jinja2
;template.render
don’t include therequest
, so I removed it from the path operation signature;template.render
are no longer adict
, but ratherkwargs
;response_class=HTMLResponse
from the signature;FileResponse
; instead, I did something like this:This allows me to click a button on the frontend and get a pdf downloaded.
I hope this helps. I wish I had something like this in the examples.