It's my first website on Azure with Python. I developed before with .NET. Sorry if my question is a basic one.
I deployed my Django website on Azure. It works fine with the DB settings in the settings.py.
Next step, I thought, is to transfer the DB settings to Azure's Application Settings - Connection Strings, what I did.
I am aware that I need the prefix MYSQLCONNSTR_connectionString1.
How can I now link my Django/Python WebApp with the Connection String in the Azure Application Settings?
I would be very grateful for help.
Just use import os to import os package in your Django code like settings.py, then get these environment variables which be defined in App settings or Connection strings of the tab Application settings on Azure portal via the code os.getenv("<environ-var-name>").
For the environment variable defined in App settings, you just pass the variable name defined like VARNAME as the argument for os.getenv().
To get the value of the environment variable defined in Connection strings, you need to add the different prefix or surfix for the variable name like VARNAME, as below.
For SQL Database, using prefix SQLAZURECONNSTR with infix symbol _ for VARNAME is SQLAZURECONNSTR_VARNAME.
For SQL Server, using prefix SQLCONNSTR with infix symbol _ for VARNAME is SQLCONNSTR_VARNAME.
For MySQL, using prefix MYSQLCONNSTR with infix symbol _ for VARNAME is MYSQLCONNSTR_VARNAME.
For Custom, using surfix MYSQLCONNSTR with infix symbol _ for VARNAME is VARNAME_CONNECTSTRING.
Hope it helps.
I couldn't connect the database via Environment Variables in Azure's Application settings - Connection Strings, though I tried various approaches. Still would be grateful for help how it can be done.
What worked instead, reading https://godjango.com/blog/working-with-environment-variables-in-python/:
Enter in Azure Application settings - App settings the single DB connection values:
KEY VALUE
DATABASE_NAME: <database>
DATABASE_USER: <dbuser>
DATABASE_PASSWORD: <dbpassword>
DATABASE_HOST: <host>
DATABASE_PORT: <port>
And in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DATABASE_NAME', ''),
'USER': os.environ.get('DATABASE_USER', ''),
'PASSWORD': os.environ.get('DATABASE_PASSWORD', ''),
'HOST': os.environ.get('DATABASE_HOST', ''),
'PORT': os.environ.get('DATABASE_PORT', ''),
}
}