1

I am trying to connect MSSQL to my Django framework to be able to send some queries. However, I am not sure which setting I should implement in my settings.py file. It keeps giving me errors.

My Django version is: 1.11.20. Python version is: 3.7.1.

I have tried different engines with different drivers but I had no luck get it working. My current attempt is like this:

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,
        },

    }
}

When I try to apply migrates, I receive this output:

    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)

Any insight is very appreciated.

Nano
  • 57
  • 1
  • 9

1 Answers1

3

Error has nothing to do with DB connection.

Django 1.11.20 is not having path yet. You should use old url patterns style ( regex )

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

If this is new project I strongly suggest using latest Django version.

iklinac
  • 14,944
  • 4
  • 28
  • 30
  • When I tried to update Django, I got this message: django-pyodbc-azure 1.11.9.0 has requirement Django<2.0,>=1.11.9, but you'll have django 2.2 which is incompatible. I need to use django-pyodbc-azure to connect to mssql right? – Nano Apr 15 '19 at 13:06
  • You have installed django-pyodbc-azure 1.11.9.0 but there is a 2.1 version that seems to be compatible with Django 2.1 https://github.com/michiya/django-pyodbc-azure – iklinac Apr 15 '19 at 13:10
  • Thanks a lot, I have last question. Now after I change the versions according to your answer, I started getting this error: django.db.utils.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)') – Nano Apr 15 '19 at 13:17
  • That is already different question, and also there is enough of answers if you search for it it on SO https://stackoverflow.com/questions/17115632/microsoftodbc-driver-manager-data-source-name-not-found-and-no-default-drive – iklinac Apr 15 '19 at 13:53
  • 1
    Thanks a lot @iklinac – Nano Apr 15 '19 at 13:57
  • `django-pyodbc-azure` isn't out for Django 2.2 (yet) - it'll be a few weeks, according to the maintainer. For now, you can use Django 2.1: `pip install -U 'django-pyodbc-azure<2.2'` – FlipperPA Apr 15 '19 at 16:00