Fastapi: Why I can't access my router?

Created on 31 Jul 2020  路  4Comments  路  Source: tiangolo/fastapi

main.py

app = FastAPI()

crud.py

from main import app

@app.get("/posts")
async def posts():
    return {"posts": 'test'}
python uvicorn main:app --reload

I cant' access '/posts'

question

All 4 comments

You're trying to return posts, but posts is the name of the function...

You're trying to return posts, but posts is the name of the function...

I updated, so, I can't access /posts

Your application is not able to "see" the crud.py. Maybe you can add the crud.py into your __init__.py and it will solve the problem, or...
Suggestion:

# main.py
from fastapi import FastAPI

from .crud import router

app = FastAPI()
app.include_router(router)

# crud.py
from fastapi import APIRouter

router = APIRouter()

@router.get("/posts")
async def posts():
    return {"posts": 'test'}

Thanks for the help here @Kludex ! :clap: :bow:

Thanks for reporting back and closing the issue :+1:

Was this page helpful?
0 / 5 - 0 ratings