当前位置: 首页 > news >正文

调和Django与Sql server2019的关系

数据库升级成sqlserver2019后,用户发现一些APP不能用了,检查发现是Django连接sqlserver的APP全部不能用了,页面打开都是500错误,进入Pycharm调试发现:
1.遇到错误1:django.db.backends.XXX'错误

  File "C:\Python\Python310\lib\site-packages\django\db\utils.py", line 202, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python\Python310\lib\site-packages\django\db\utils.py", line 121, in load_backend
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: 'sql_server.pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'

按网上提示,修改连接字符串是绝对不好使的,安装相关的库也不好使。
C:\Users\Administrator>pip install django-mssql-backend
Collecting django-mssql-backend
  Downloading django_mssql_backend-2.8.1-py3-none-any.whl.metadata (7.4 kB)
Requirement already satisfied: pyodbc>=3.0 in c:\python\python310\lib\site-packages (from django-mssql-backend) (4.0.32)
Downloading django_mssql_backend-2.8.1-py3-none-any.whl (52 kB)
   ---------------------------------------- 52.4/52.4 kB 669.1 kB/s eta 0:00:00
Installing collected packages: django-mssql-backend
Successfully installed django-mssql-backend-2.8.1

[notice] A new release of pip is available: 23.3.2 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

C:\Users\Administrator>pip install django-pyodbc-azure
Requirement already satisfied: django-pyodbc-azure in c:\python\python310\lib\site-packages (2.1.0.0)
Requirement already satisfied: Django<2.2,>=2.1.0 in c:\python\python310\lib\site-packages (from django-pyodbc-azure) (2.1.15)
Requirement already satisfied: pyodbc>=3.0 in c:\python\python310\lib\site-packages (from django-pyodbc-azure) (4.0.32)
Requirement already satisfied: pytz in c:\python\python310\lib\site-packages (from Django<2.2,>=2.1.0->django-pyodbc-azure) (2022.1)

[notice] A new release of pip is available: 23.3.2 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

C:\Users\Administrator>python
>>> import django
>>> print(django.get_version())

2.1.15

解决方案:必须要升级Django才能解决问题

C:\Users\Administrator>pip install --upgrade django==3.2.0
Collecting django==3.2.0
  Using cached Django-3.2-py3-none-any.whl.metadata (3.9 kB)
Requirement already satisfied: asgiref<4,>=3.3.2 in c:\python\python310\lib\site-packages (from django==3.2.0) (3.8.1)
Requirement already satisfied: pytz in c:\python\python310\lib\site-packages (from django==3.2.0) (2022.1)
Requirement already satisfied: sqlparse>=0.2.2 in c:\python\python310\lib\site-packages (from django==3.2.0) (0.4.2)
Requirement already satisfied: typing-extensions>=4 in c:\python\python310\lib\site-packages (from asgiref<4,>=3.3.2->django==3.2.0) (4.5.0)
Using cached Django-3.2-py3-none-any.whl (7.9 MB)
Installing collected packages: django
  Attempting uninstall: django
    Found existing installation: Django 2.1.3
    Uninstalling Django-2.1.3:
      Successfully uninstalled Django-2.1.3
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
django-pyodbc-azure 2.1.0.0 requires Django<2.2,>=2.1.0, but you have django 3.2 which is incompatible.
Successfully installed django-3.2

C:\Users\Administrator>pip install --upgrade requests
Requirement already satisfied: requests in c:\python\python310\lib\site-packages (2.27.1)
Collecting requests
  Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\python\python310\lib\site-packages (from requests) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in c:\python\python310\lib\site-packages (from requests) (3.3)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\python\python310\lib\site-packages (from requests) (1.26.9)
Requirement already satisfied: certifi>=2017.4.17 in c:\python\python310\lib\site-packages (from requests) (2021.10.8)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
Installing collected packages: requests
  Attempting uninstall: requests
    Found existing installation: requests 2.27.1
    Uninstalling requests-2.27.1:
      Successfully uninstalled requests-2.27.1
Successfully installed requests-2.32.3

[notice] A new release of pip is available: 23.3.2 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip

C:\Users\Administrator>python.exe -m pip install --upgrade pip
Requirement already satisfied: pip in c:\python\python310\lib\site-packages (23.3.2)
Collecting pip
  Downloading pip-25.0.1-py3-none-any.whl.metadata (3.7 kB)
Downloading pip-25.0.1-py3-none-any.whl (1.8 MB)
   ---------------------------------------- 1.8/1.8 MB 6.9 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 23.3.2
    Uninstalling pip-23.3.2:
      Successfully uninstalled pip-23.3.2
Successfully installed pip-25.0.1

2.遇到的错误2:升级django后,访问APP的时候,不再提示sqlserver连接错误了,但出新错误:

RequestsDependencyWarning: urllib3 (1.26.9) or chardet (5.1.0)/charset_normalizer (2.0.12) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported "
System check identified some issues:
更新 urllib3
C:\Users\Administrator>pip install --upgrade urllib3
Requirement already satisfied: urllib3 in c:\python\python310\lib\site-packages (1.26.9)
Collecting urllib3
  Downloading urllib3-2.3.0-py3-none-any.whl.metadata (6.5 kB)
Downloading urllib3-2.3.0-py3-none-any.whl (128 kB)
Installing collected packages: urllib3
  Attempting uninstall: urllib3
    Found existing installation: urllib3 1.26.9
    Uninstalling urllib3-1.26.9:
      Successfully uninstalled urllib3-1.26.9
Successfully installed urllib3-2.3.0

3.遇到的错误3:
from django.shortcuts import HttpResponseRedirect,Http404,HttpResponse,render_to_response
ImportError: cannot import name 'render_to_response' from 'django.shortcuts' (C:\Python\Python310\lib\site-packages\django\shortcuts.py)

这个需要使用 render 代替 render_to_response。render_to_response已经被废弃
render 方法可接收三个参数,一是request参数,二是待渲染的html模板文件,三是保存具体数据的字典参数。它的作用就是将数据填充进模板文件,最后把结果返回给浏览器。

 4.遇到的错误4:
?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc
        HINT: Add a scheme (e.g. https://) or netloc (e.g. example.com).
CORS_ORIGIN_WHITELIST = (
    ['http://127.0.0.1:*']

)
将原来的值'http://127.0.0.1','http://192.168.0.1'改成['http://127.0.0.1','http://192.168.0.1'],通过


5.遇到新错误5:
django.core.exceptions.ImproperlyConfigured: WSGI application 'waitress.server:call_in_application' could not be loaded; Error importing module.
因为使用的是IIS,没有使用waitress, 直接用#注释掉

运行不正常的APP终于能正常运行。

相关文章:

  • 【工具】huggingface 模型下载过程
  • Redis主从集群和哨兵集群
  • Go语言的负载均衡
  • 电子病历系统如何设计?
  • MySQL 性能优化:索引优化 + 读写分离 + Redis 缓存,TPS 提升 175% 实战解析
  • 生成式人工智能爆发:未来“人工”会被取代吗?
  • 【ARM】MDK-STM32g0xx.h文件与Define规则记录
  • C语言中qsort函数的详解,以及模拟
  • 数据通信与计算机网络——绪论
  • Spring组件初始化扩展点:BeanPostProcessor
  • CMake学习笔记(二):变量设值,源文件/文件查找
  • 为什么在域名注册后还需要进行解析?
  • 大模型时代:主流训练、微调、推理与部署框架深度解析及对比
  • Spring设计模式 八股速记 高层模块底层模块 依赖倒置原则 开闭原则 接口隔离原则
  • 洛谷每日1题-------Day22__P1421 小玉买文具
  • 技术债务的隐患:何时重构,何时妥协?
  • 【认知框架重构】
  • 【R语言】FetchData函数:处理单细胞数据的灵活高效工具
  • 实验篇| Nginx环境搭建-安全配置
  • Linux:(socket套接字——TCP协议)
  • 江西3人拟提名为县(市、区)长候选人
  • 中华人民共和国和巴西联邦共和国关于强化携手构建更公正世界和更可持续星球的中巴命运共同体,共同维护多边主义的联合声明
  • 四部门:到2025年底,全国行政村5G通达率超过90%
  • 美国三大指数全线高开:纳指涨逾4%,大型科技股、中概股大涨
  • 国务院新闻办公室发布《新时代的中国国家安全》白皮书
  • 新疆交通运输厅厅长西尔艾力·外力履新吐鲁番市市长候选人