fastapi: CANNOT RENDER HTML IN FASTAPI
I 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”) `
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 18 (5 by maintainers)
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.htmltoitem.jinja2;template.renderdon’t include therequest, so I removed it from the path operation signature;template.renderare no longer adict, but ratherkwargs;response_class=HTMLResponsefrom 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.
I went through the same problem with the post request. I tend to agree that the Fastapi documentation doesn’t explain how to link the post request with an html web page. It shows what you need in Fastapi only without the other part. I figured out how to do it by myself. I asked that question too but received no answer for it. There is a need of an example to do this in the documentation. I can contribute on this aspect. I generated web pages with fastapi and it works similar to Flask. Had to relearn to program it in Fastapi code.
You have to pass the request as part of the key-value pairs in the context for Jinja2.
The fix is quite easy you just need to add Request in query parameters,
Then pass all the data inside the dictionary that you want to render.