Chapter 4
Chapter 4
- different kinds of response:
JSONResponse,HTMLResponse,PlainTextResponse,FileResponse.- base class for different response
- templates: jinja2
- fastapi can use jinja2's template mechanism
- Serving static Assets
- FastAPI facilitates the user of such static files from a location configured for the purpose. These files should be put in a folder named static. It should be in the same folder where the application code script as well as the templates folder resides
- The path pointing to this static folder is mounted as a subaplication. The
mount()method of the Application class defines a/staticroute mapped to its path - The
StaticFilesclass (imported from thefastapi.staticfilesmodule) maps the static directory to the/staticroute. By mounting a static subapplication, the static content becomes easy to deliver, process, and cache. - Whenever we want to use any of these static files, their path is obtained with the
url_for()function provided by the jinja2 template language. This can help us avoid referring to the file by its local path and causing a security concern. - [???]TODO:
- Not sure what does this
requestdo here. - also, don't really know the difference between
localhost:8080/formv.s.localhost:8080/form/(with and without the last/). The first will definitely get routed byapp.get("/{name}"), but what is the difference?