update. 优化细节

This commit is contained in:
2026-04-11 05:28:56 +08:00
parent ab6c0a9b49
commit a9de2d1f68
+9 -8
View File
@@ -27,8 +27,8 @@ from django.urls import path, include
from loguru import logger
from ninja.errors import ValidationError
from ninja_extra import NinjaExtraAPI
from ninja_jwt.controller import NinjaJWTDefaultController
from ninja_jwt.authentication import JWTAuth
from ninja_jwt.controller import NinjaJWTDefaultController
from base.views import custom_upload_file
from common.common_response import CommonResponse
@@ -64,24 +64,23 @@ def auto_load_routers(package_name_prefix=None):
# 检查是否存在 'router' 变量
if hasattr(module, 'router'):
router = getattr(module, 'router')
# 使用文件夹名称 (modname) 作为 URL 前缀
# 例如: 文件夹 'authorize' -> 前缀 '/authorize'
url_prefix = f"/{modname}"
api_v1.add_router(url_prefix, router)
print(f"✅ 自动注册: {url_prefix} (来自 {full_module_path})")
logger.info(f"✅ 自动注册: {url_prefix} (来自 {full_module_path})")
else:
print(f"⚠️ 跳过: {modname} (在 {full_module_path} 中未找到 'router' 变量)")
logger.warning(f"⚠️ 跳过: {modname} (在 {full_module_path} 中未找到 'router' 变量)")
except ImportError as e:
# 如果文件夹里没有 views.py,会报 ImportError,这是正常的,跳过即可
if "No module named" in str(e) and "views" in str(e):
print(f"️ 跳过: {modname} (没有 views.py 模块)")
logger.warning(f"️ 跳过: {modname} (没有 views.py 模块)")
else:
print(f"❌ 导入错误 {modname}: {e}")
logger.error(f"❌ 导入错误 {modname}: {e}")
except Exception as e:
print(f"❌ 注册失败 {modname}: {e}")
logger.error(f"❌ 注册失败 {modname}: {e}")
class UnifiedNinjaAPI(NinjaExtraAPI):
@@ -140,7 +139,9 @@ api_v1 = UnifiedNinjaAPI(version='1.0.0', auth=JWTAuth())
api_v1.register_controllers(NinjaJWTDefaultController)
auto_load_routers()
logger.debug(api_v1.urls)
# logger.debug(api_v1.urls)
@api_v1.exception_handler(ValidationError)