广告区
广告区
关注本站官方公众号:程序员总部,领取三大福利!
福利一:python和前端辅导
福利二:进程序员交流微信群,专属于程序员的圈子
福利三:领取全套零基础视频教程(python,java,前端,php)
发布于2022-05-18 03:20 阅读(1057) 评论(0) 点赞(26) 收藏(4)
如何设置django项目可以从多个url进入admin管理登录页面,我想根据不同的url使用不同的数据库
但这似乎不起作用。当我访问admin1-3的登录页面时,他将被重置为admin
即使我在数据库路由判断中返回,在代码所在的地方登录还是会报错
错误截图
我的想法错了吗?能否给点建议,非常感谢。
设置.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'admin',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306
},
'adminx1': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'adminx1',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306
},
'adminx2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'adminx2',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306
},
'adminx3': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'adminx3',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '127.0.0.1',
'PORT': 3306
}
}
网址.py
urlpatterns = [
path('app_test/', include('apps.app_test.urls')),
path('admin/', admin.site.urls),
path('admin1/', admin.site.urls),
path('admin2/', admin.site.urls),
path('admin3/', admin.site.urls),
]
中间件.py
import threading
from django.conf import settings
request_cfg = threading.local()
class AppTestMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# Code to be executed for each request before
# the view (and later middleware) are called.
request_cfg.request = request
response = self.get_response(request)
if hasattr(request_cfg, 'request'):
del request_cfg.request
# Code to be executed for each request/response after
# the view is called.
return response
class AuthRouter:
"""
A router to control all database operations on models in the
auth and contenttypes applications.
"""
route_app_labels = {}
def _default_db(self):
if hasattr(request_cfg, 'request'):
try:
database_path_name = request_cfg.request.path.split('/')[1]
except IndexError:
return 'default'
if request_cfg.request.GET.get('next') == '/admin1/':
return 'admin1'
database = settings.DATABASES.get(database_path_name)
return database and database['NAME']
return None
def db_for_read(self, model, **hints):
"""
Attempts to read auth and contenttypes models go to auth_db.
"""
default_db = self._default_db()
if default_db:
return default_db
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
def db_for_write(self, model, **hints):
"""
Attempts to write auth and contenttypes models go to auth_db.
"""
default_db = self._default_db()
if default_db:
return default_db
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
关注本站官方公众号:程序员总部,领取三大福利!
福利一:python和前端辅导
福利二:进程序员交流微信群,专属于程序员的圈子
福利三:领取全套零基础视频教程(python,java,前端,php)
关注公众号回复python,免费领取 全套python视频,回复充值+你的账号,免费为您充值1000积分
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/1523235/b704de9423187746cd99/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!