Django app deployed on Azure not connecting to Database

Ian Grundling 25 Reputation points
2024-12-09T08:00:24.04+00:00

Hi all,

I am facing issues deploying a Django app on Azure, the deployment builds and runs successfully, when I do python manage.py migrate the database migrate, then I create a user and try to sign in then I get server 500. I have done some troubleshooting already and saw that the Django app is not connecting to the database.

Azure Database for PostgreSQL
{count} votes

Accepted answer
  1. Vijayalaxmi Kattimani 660 Reputation points Microsoft Vendor
    2024-12-09T13:16:46.9066667+00:00

    Hi @Ian Grundling,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    As we understand that, you are unable to deploy a Django app on Azure and facing a connectivity issue. One possible reason could be that there is an issue with your database configuration or connectivity of your Django app on Azure.

    Please find the below mentioned steps to diagnose and resolve the problem:

    Make sure that, your database settings are correctly configured in settings.py for Azure. Please refer to the below mentioned example for PostgreSQL and replace placeholders with actual values.

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'your_database_name',
            'USER': 'your_database_user',
            'PASSWORD': 'your_password',
            'HOST': 'your_server_name.postgres.database.azure.com',
            'PORT': '5432',
            'OPTIONS': {
                'sslmode': 'require',  # Ensures SSL is used
            },
        }
    }
    
    • Ensure that, environment variables are properly accessible in Azure. you can set them in the Configuration section of your App Service.
    import os
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': os.getenv('DB_NAME'),
            'USER': os.getenv('DB_USER'),
            'PASSWORD': os.getenv('DB_PASSWORD'),
            'HOST': os.getenv('DB_HOST'),
            'PORT': os.getenv('DB_PORT', '5432'),
            'OPTIONS': {
                'sslmode': 'require',
            },
        }
    }
    
    • You can check for the logs. In your Azure Portal, go to your App Service. Navigate to “Diagnose and solve problems”--- > “Application Logs”. Check for the error message related to database connectivity.
    • Check if your Django app can reach the database, From your local machine, use a tool like pgAdmin to connect to the database using the same credentials. Use Azure's Connection Strings in the Database Resource settings for verification. Ensure Firewall Rules allow access from your App Service. You can test the connection string with below mentioned code. python manage.py dbshell
    • If you are facing migration related issues, verify migrations applied correctly with this python manage.py showmigrations. If tables are missing, apply migrations explicitly with this code python manage.py migrate.
    • In order to resolve Azure App Service Issues, Check if the Managed Identity is enabled and granted appropriate database roles if you're using Azure's Managed Identity for database access. If you are using a different authentication method ensure Django supports it.

    Please refer to the below mentioned links.

    https://learn.microsoft.com/en-us/training/modules/django-deployment/?source=recommendations

    https://github.com/microsoft/Oryx/wiki/Django-Tips

    Here are the links of some parallel threads that might help you :

    https://learn.microsoft.com/en-us/answers/questions/1191945/after-deployment-internal-server-error-on-my-web-a

    https://learn.microsoft.com/en-us/answers/questions/904896/app-page-shows-internal-server-error-while-deployi

    I hope, This response will address your query and helped you to overcome on your challenges.

    If this answers your query, do click Accept Answer and Yes for was this answer helpful. And, if you have any further query do let us know.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.