AnishChapagai-2147 avatar image
0 Votes"
AnishChapagai-2147 asked AnishChapagai-2147 commented

What is the best way to connect to database from azure webapp?

I have a django project in hosted in Azure WebApp Service. I also have a Sql Server and database within Azure. I can connect it on <databasename> from my local machine. I now want my webapp to connect to that same database but I don't want to query from public url since that I think will route traffic from over the internet. I tried creating a virtual network, bounded the database's private link on that virtual network resource and replaced my host with that private link( its like XX.X.X.X) in application's environment variable. Now this will not work from my local machine obviously, but it should from within Azure network's private link, shouldn't it? But I am getting error. Here's the traceback.


Request Method: GET
Request URL: http://<appname><a_path>/

Django Version: 4.0.4
Python Version: 3.9.7
Installed Applications:
Installed Middleware:

Traceback (most recent call last):
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/core/handlers/", line 55, in inner
    response = get_response(request)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/core/handlers/", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/tmp/8da4082417a153e/<a_path>/", line 16, in index
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/", line 320, in __iter__
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/", line 1507, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/", line 57, in __iter__
    results = compiler.execute_sql(
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/models/sql/", line 1348, in execute_sql
    sql, params = self.as_sql()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 188, in as_sql
    supports_offset_clause = self.connection.sql_server_version >= 2012
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/", line 49, in __get__
    res = instance.__dict__[] = self.func(instance)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 445, in sql_server_version
    with self.temporary_connection() as cursor:
  File "/opt/python/3.9.7/lib/python3.9/", line 119, in __enter__
    return next(self.gen)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/", line 639, in temporary_connection
    with self.cursor() as cursor:
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/", line 284, in cursor
    return self._cursor()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 246, in _cursor
    conn = super()._cursor()
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/", line 260, in _cursor
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/", line 244, in ensure_connection
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/utils/", line 26, in inner
    return func(*args, **kwargs)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/django/db/backends/base/", line 225, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 324, in get_new_connection
    connstr = encode_connection_string(cstr_parts)
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 53, in encode_connection_string
    return ';'.join(
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 54, in <genexpr>
    '%s=%s' % (k, encode_value(v))
  File "/tmp/8da4082417a153e/antenv/lib/python3.9/site-packages/mssql/", line 78, in encode_value
    if ';' in v or v.strip(' ').startswith('{'):

Exception Type: TypeError at /<a_path>/
Exception Value: argument of type 'NoneType' is not iterable

I have migrated proper tables and it works from my local machine, so I'm assuming the problem is from my deployment side. Am I not connecting to right link or not configuring something right?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AnishChapagai-2147 Thank you for your question regarding connecting your web app to Azure Web App to your Azure SQL DB privately.

Based on your current verbatim it's tough to understand if you've completed 100% of the necessary configuration.

We have an architecture document that covers this scenario. Can you please review it and then let us know if there are any items that you missed while configuring or if you are still receiving errors after verifying proper configuration?

We look forward to your reply.

1 Vote 1 ·

1 Answer

nhcloud avatar image
0 Votes"
nhcloud answered AnishChapagai-2147 commented

Hello @AnishChapagai-2147
As @brtrach-MSFT said, I would suspect you are missing some configuration, enabling the web app and SQL is very easy in azure, I have summarized the steps.

If both Web App and Database are in the same region (same VNET), maybe multiple subnets, here are the steps,

  1. Enable vNET with one or more subnets (maybe 1 for web and another one for DB)

  2. For Web APpp, From the network tab enable VNET

  3. For SQL, From the network tab,
    3(a). from public access, either disable or "selected network" --in case you want to use withing your browser without VM in the same network
    3(b). from private access, enable the private link

please note if they are located in the different regions then you need to peer the networks as well as create a virtual link to your database

image.png (88.6 KiB)
image.png (58.2 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you very much. Turns out I didn't have much knowledge on Virtual Networks and Subnets as a whole. After reading some docs and YouTube videos I found out I had to create subnets for App and Database Server under "same" resource + "same" region + same Virtual Network and then substitute the database's private link.

0 Votes 0 ·