如何将MSSQL连接到Django

时间:2019-04-15 12:32:44

标签: python sql-server django

我正在尝试将MSSQL连接到我的Django框架,以便能够发送一些查询。但是,我不确定应该在settings.py文件中实现哪个设置。它不断给我错误。

我的Django版本是:1.11.20。 Python版本是:3.7.1。

我尝试过使用不同的驱动程序使用不同的引擎,但是我没有运气。我目前的尝试是这样的:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'DB_name',
        'Host': 'my_host',
        'port': '1433',
        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
            'unicode_results': True,
        },

    }
}

当我尝试应用迁移时,我收到以下输出:

    Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main                                                                                                                        execute_from_command_line(sys.argv)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 327, in execute
    self.check()
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\commands\migrate.py", line 62, in _run_checks
    issues.extend(super(Command, self)._run_checks(**kwargs))
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\management\base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 26, in check_resolver
    return check_method()
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 256, in check
    for pattern in self.url_patterns:
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 407, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\utils\functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\ProgramData\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 400, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\ProgramData\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\ahk\Desktop\Django Project\mysite\mysite\urls.py", line 17, in <module>
    from django.urls import path
ImportError: cannot import name 'path' from 'django.urls' (C:\ProgramData\Anaconda3\lib\site-packages\django\urls\__init__.py)

任何见识都非常感谢。

1 个答案:

答案 0 :(得分:3)

错误与数据库连接无关。

Django 1.11.20尚无路径。 您应该使用old url patterns style(regex)

from django.conf.urls import url
urlpatterns = [
    url('',views.home,name='home')
]

如果这是一个新项目,我强烈建议您使用最新的Django版本。