Can't get sql server to work with django azure web app

I've been trying for 3 days now and can't get this to work. I am following this tutorial from Microsoft docs: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-python-create-deploy-django-app

EDIT: I am using the Windows command line option, not Visual Studio. I am starting with the python / django PTVS template in azure market as suggested in the tutorial.

The app works fine with sql lite, but when I change it to sql server it stops working with the following error:

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'
Error was: No module named 'sql_server'
2017-04-27 04:34:34.525084: wfastcgi.py 2.1.1 closed

      

The app works for me locally, connecting to the same remote sql azure db, so the problem is definitely azure. I got the same error locally until I installed pyodbc-azure ( https://github.com/michiya/django-pyodbc-azure ). As suggested by Microsoft docs and library docs, the following is the database connection specification in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'sql_server.pyodbc',
        'NAME': 'django-myorg',
        'USER': 'riz',
        'PASSWORD': '#######',
        'HOST': 'django-myorg.database.windows.net',
        'PORT': '',

        'OPTIONS': {
            'driver': 'ODBC Driver 13 for SQL Server',
        },
    },
}

      

This is my third attempt at setting this up and I am frustrated by the lack of up-to-date documentation on the Microsoft part.

I tried to troubleshoot wherever I could by following this guide: https://docs.microsoft.com/en-us/azure/app-service-web/web-sites-python-configure . I tried building wheels for all the required libraries but still had the same problem. I have a feeling that this is one of the pyodbc libraries that Azure cannot get with pip.

Update: I removed the .skipPythonDepolyment which was enabled by default in the PTVS template provided by azure for django. This prompted azure to actually try and install the libraries listed in the .txt requirements file. This is the most recent error according to pip.log:

running build

running build_ext

building 'pyodbc' extension

error: Unable to find vcvarsall.bat

      

it looks like it's trying to build pyodbc but doesn't work. I don't know what I can do at this moment.

+3


source to share


1 answer


I have a similar setup for you and have figured out for months on how to get Django to work on Azure. There are a few things you should know ahead of time because they can help solve your problem:

Python version

I am having a couple of problems with my web.config files because it seems that Django apps are not loading using the Python version specified in "runtime.txt" as they say. They use 2.7 by default. To run Django using Python 3.4, you need to manually replace the web.config file with the version you want, and make sure it points to the correct Django version. I believe a Django app has three web.config files by default, one for 2.7, one for 3.4, and one titled web.config

. Try copying file 3.4 to web.config

, which is the only file that Azure uses.

Installing a gun from wheels

You will need to install many libraries from the wheels, most of which can be found at http://www.lfd.uci.edu/~gohlke/pythonlibs/ . Keep in mind that you need to use Python 3.4 32-bit on Azure 32-bit (unless you changed the default python and environment), and in addition, you need to replace the middle tag with none

or it will break on Azure:

numpy‑1.13.1+mkl‑cp34‑cp34m‑win32.whl

changes to numpy‑1.13.1+mkl‑cp34‑none‑win32.whl

To make it easier to install from wheels, I added a folder wheelhouse

to the root folder of the application on Azure and moved my wheels there. Then in requirements.txt

I point to the exact wheel file in this wheelhouse that I want to install.

Django version



django-pyodbc-azure

required, and in my experience the latest build works with Django 1.11. However, if I recall correctly, by default the Django app on the market just says django

in its requirements.txt

file, so if you're having trouble connecting after a successful install django-pyodbc-azure

, try specifying the Django version. I use the following and it works:

django>=1.11.1,<1.12
pyodbc from wheel
D:\home\site\wwwroot\wheelhouse\pyodbc-4.0.16-cp34-none-win32.whl
django-pyodbc-azure==1.11.0

      

Check your pip build output to make sure it re-installs the correct version of Django if you don't already have 1.11. I recommend that you point out in your requirements file that django should not be upgraded to 1.12 until you are ready, because otherwise it might get upgraded when the next release comes out and that will break the connection to django-pyodbc-azure

.

Database connection

The connection to my database is slightly different. If you're using an Azure SQL Database, try one or more of the following:

  • Specify port 1433
  • Add tcp:

    before your database host eg. tcp:django-myorg.database.windows.net

    - and make sure your SQL server is actually named django-myorg

    , this is separate from your database name
  • Try changing your driver to SQL Server Native Client 11.0

Chances are your settings are fine if they work on your computer and the problem is creating the modules you need, just in case.

And I have a heart, it took me several months to set up - the process can certainly be simplified.

+2


source







All Articles