fastapi has no attribute 'routes'
时间: 2023-10-03 15:08:40 浏览: 192
FastAPI does not have an attribute named 'routes'. The correct attribute to use in FastAPI is 'app'. FastAPI uses the Starlette framework, and you can define routes using the 'app' instance.
Here's an example of defining routes in FastAPI:
```python
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
```
In this example, we define two routes using the `app` instance: one for the root path ("/") and another for "/items/{item_id}". You can use various HTTP methods like `get`, `post`, `put`, `delete`, etc., to define different routes.
Remember to import the necessary modules and start the application using the `uvicorn` server or any other compatible server.
阅读全文