Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example - 404 Error #23

Closed
AlexTheByte opened this issue Nov 28, 2020 · 12 comments
Closed

Example - 404 Error #23

AlexTheByte opened this issue Nov 28, 2020 · 12 comments

Comments

@AlexTheByte
Copy link

AlexTheByte commented Nov 28, 2020

Hi, before integrating your solution into my project, I tried to execute your example :

image

The application start well :

image

And the data are injected into the database :

image

But when I try to navigate to 127.0.0.1:8000/admin, I have an 404 error :

image

image

Otherwise, 127.0.0.1:8000/admin/docs works

My configuration :

  • os: Ubuntu 18.04
  • docker: 19.03.13
  • docker-compose: 1.26.0

If you need more informations, do not hesitate to ask me.

Thank you !

@long2ice
Copy link
Collaborator

long2ice commented Nov 28, 2020

You are not mistaken, restful api server is runing normally, next you do is just run front, see https://github.com/long2ice/restful-admin

@AlexTheByte
Copy link
Author

Well, I missed this detail ! Thank you !

@UsamaHaide0786
Copy link

im also trying to integrate it but getting few errors like
-ImportError: cannot import name 'QuerySet' from 'tortoise'
-TORTOISE_ORM not defined

any help please i new to fastapi

my code looks like this

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from app.api.api_v1.api import api_router
from app.core.config import settings
from fastapi.staticfiles import StaticFiles
from fastapi_admin.factory import app as admin_app
from tortoise.contrib.starlette import register_tortoise
from .auto_save import AutoSave

app = FastAPI(
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
# debug=True
)
register_tortoise(app, config=TORTOISE_ORM, generate_schemas=True)
app.mount('/admin', admin_app)
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")

Set all CORS enabled origins

if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=[""],
allow_headers=["
"],
)

@app.on_event("startup")
async def startup():
await admin_app.init(
admin_secret="test",
permission=True,
site=Site(
name="FastAPI-Admin DEMO",
login_footer="FASTAPI ADMIN - FastAPI Admin Dashboard",
login_description="FastAPI Admin Dashboard",
locale="en-US",
locale_switcher=True,
theme_switcher=True,
))

app.include_router(api_router, prefix=settings.API_V1_STR)

@long2ice
Copy link
Collaborator

long2ice commented Jan 6, 2021

Can't see any wrong, you can reference examples

@UsamaHaide0786
Copy link

Everything is as in examples i compared all the files i dont know what would be the problem @long2ice please help me to solve this issue if you can i should be thank full

main.py

from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from app.api.api_v1.api import api_router
from app.core.config import settings
from fastapi.staticfiles import StaticFiles
from fastapi_admin.factory import app as admin_app
from tortoise.contrib.starlette import register_tortoise
from .auto_save import AutoSave

app = FastAPI(
title=settings.PROJECT_NAME, openapi_url=f"{settings.API_V1_STR}/openapi.json"
# debug=True
)
register_tortoise(app, config=TORTOISE_ORM, generate_schemas=True)
app.mount('/admin', admin_app)
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")

Set all CORS enabled origins

if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=[""],
allow_headers=["
"],
)

@app.on_event("startup")
async def startup():
pass
await admin_app.init(
admin_secret="test",
permission=True,
site=Site(
name="FastAPI-Admin DEMO",
login_footer="FASTAPI ADMIN - FastAPI Admin Dashboard",
login_description="FastAPI Admin Dashboard",
locale="en-US",
locale_switcher=True,
theme_switcher=True,
))

app.include_router(api_router, prefix=settings.API_V1_STR)

ERROR

Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self.kwargs)
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn\subprocess.py", line 61, in subprocess_started
target(sockets=sockets)
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn_impl\asyncio.py", line 47, in run
loop.run_until_complete(self.serve(sockets=sockets))
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 573, in run_until_complete
return future.result()
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn_impl\asyncio.py", line 54, in serve
config.load()
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn\config.py", line 306, in load
self.loaded_app = import_from_string(self.app)
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn\importer.py", line 23, in import_from_string
raise exc from None
File "d:\taskeen\env_taskeen\lib\site-packages\uvicorn\importer.py", line 20, in import_from_string
module = importlib.import_module(module_str)
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\importlib_init
.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1006, in _gcd_import
File "", line 983, in _find_and_load
File "", line 967, in find_and_load_unlocked
File "", line 677, in load_unlocked
File "", line 728, in exec_module
File "", line 219, in call_with_frames_removed
File ".\app\main.py", line 6, in
from fastapi_admin.factory import app as admin_app
File "d:\taskeen\env_taskeen\lib\site-packages\fastapi_admin_init
.py", line 1, in
from . import routes # noqa: F401
File "d:\taskeen\env_taskeen\lib\site-packages\fastapi_admin\routes_init
.py", line 3, in
from ..depends import jwt_required
File "d:\taskeen\env_taskeen\lib\site-packages\fastapi_admin\depends.py", line 12, in
from .factory import app
File "d:\taskeen\env_taskeen\lib\site-packages\fastapi_admin\factory.py", line 11, in
from .filters import SearchFilter
File "d:\taskeen\env_taskeen\lib\site-packages\fastapi_admin\filters.py", line 3, in
from tortoise import QuerySet
ImportError: cannot import name 'QuerySet' from 'tortoise' (d:\taskeen\env_taskeen\lib\site-packages\tortoise_init.py)

@long2ice
Copy link
Collaborator

long2ice commented Jan 6, 2021

What's the tortoise-orm version? Keep it latest.

@UsamaHaide0786
Copy link

(0.16.19)
thats the version i have have and also the latest one

@long2ice
Copy link
Collaborator

long2ice commented Jan 6, 2021

That's a bug and fixed in latest dev branch.

@UsamaHaide0786
Copy link

-NameError: name 'TORTOISE_ORM' is not defined
this one still there do i need to define them by my own @long2ice

@AlexTheByte
Copy link
Author

AlexTheByte commented Jan 6, 2021

Hi,

Well, I have the same issue.

image

Even after re-installing all of my project. :(

@AlexTheByte
Copy link
Author

@UsamaHaide0786 Yes you need to define your own TORTOISE_ORM.

Like this :

TORTOISE_ORM = { "connections": { "default": { "engine": "tortoise.backends.mysql", "credentials": { "host": os.getenv("DB_HOST"), "port": os.getenv("DB_PORT"), "user": os.getenv("DB_USER"), "password": os.getenv("DB_PASSWORD"), "database": os.getenv("DB_NAME"), } } }, "apps": { "models": { "models": [ "settings.dashboard", "api.v1.models.models" ], "default_connection": "default", "generate_schemas": True, "add_exception_handlers": True, } }, }

@UsamaHaide0786
Copy link

@AlexTheByte Really appreciate you guys and thank full for all the help (Quick Note: Im Using PostgreSQL)
Brother as I Updated TORTOISE_ORM according to you
This comes out

?[31mERROR?[0m: Application startup failed. Exiting.
?[33mWARNING?[0m: StatReload detected file change in 'app\main.py'. Reloading...
?[32mINFO?[0m: Started server process [?[36m12128?[0m]
?[32mINFO?[0m: Waiting for application startup.
?[31mERROR?[0m: Traceback (most recent call last):
File "d:\taskeen\env_taskeen\lib\site-packages\starlette\routing.py", line 526, in lifespan
async for item in self.lifespan_context(app):
File "d:\taskeen\env_taskeen\lib\site-packages\starlette\routing.py", line 467, in default_lifespan
await self.startup()
File "d:\taskeen\env_taskeen\lib\site-packages\starlette\routing.py", line 502, in startup
await handler()
File "d:\taskeen\env_taskeen\lib\site-packages\tortoise\contrib\starlette_init_.py", line 81, in init_orm
await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules)
File "d:\taskeen\env_taskeen\lib\site-packages\tortoise_init_.py", line 593, in init
await cls._init_connections(connections_config, create_db)
File "d:\taskeen\env_taskeen\lib\site-packages\tortoise_init
.py", line 387, in _init_connections
connection = client_class(**db_params)
File "d:\taskeen\env_taskeen\lib\site-packages\tortoise\backends\mysql\client.py", line 75, in init
self.port = int(port) # make sure port is int type
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

?[31mERROR?[0m: Application startup failed. Exiting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants