Django Azure Server Error 500

Corentin 1 Reputation point
2022-10-24T06:34:56.42+00:00

Hello,

I followed this tutorial ("https://www.youtube.com/watch?v=D6Wyk9q2JM0&list=PLlrxD0HtieHjHCQ0JB_RrhbQp_9ccJztr&index=23") to deploy my DjangoAzure-project.
I have "Server Error (500)" in response even when the project works locally.

EDIT : Thanks to the first answer i now know that azure is not the problem.

Now i can't go further in investigating why it's not working without help.

after 6 hours more on it still can't deploy this:
here's my settings :

from pathlib import Path  
  
Build paths inside the project like this: BASE_DIR / 'subdir'.  
BASE_DIR = Path(__file__).resolve().parent.parent  
  
  
 Quick-start development settings - unsuitable for production  
 See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/  
  
SECRET_KEY = 'SECURITY WARNING: keep the secret key used in production secret!'  
  
 SECURITY WARNING: don't run with debug turned on in production!  
DEBUG = False  
  
ALLOWED_HOSTS = [  
    '.azurewebsites.net'  
]  
 Application definition  
  
INSTALLED_APPS = [  
    'django.contrib.admin',  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.messages',  
    'django.contrib.staticfiles',  
    'azurecloud.apps.AzurecloudConfig'  
]  
  
MIDDLEWARE = [  
    'django.middleware.security.SecurityMiddleware',  
    'whitenoise.middleware.WhiteNoiseMiddleware',  
    'django.contrib.sessions.middleware.SessionMiddleware',  
    'django.middleware.common.CommonMiddleware',  
    'django.middleware.csrf.CsrfViewMiddleware',  
    'django.contrib.auth.middleware.AuthenticationMiddleware',  
    'django.contrib.messages.middleware.MessageMiddleware',  
    'django.middleware.clickjacking.XFrameOptionsMiddleware',  
]  
  
ROOT_URLCONF = 'azuredjango_project.urls'  
  
TEMPLATES = [  
    {  
        'BACKEND': 'django.template.backends.django.DjangoTemplates',  
        'DIRS': [],  
        'APP_DIRS': True,  
        'OPTIONS': {  
            'context_processors': [  
                'django.template.context_processors.debug',  
                'django.template.context_processors.request',  
                'django.contrib.auth.context_processors.auth',  
                'django.contrib.messages.context_processors.messages',  
            ],  
        },  
    },  
]  
  
WSGI_APPLICATION = 'azuredjango_project.wsgi.application'  
  
  
Database  
https://docs.djangoproject.com/en/4.1/ref/settings/#databases  
  
DATABASES = {  
    'default': {  
        'ENGINE': 'django.db.backends.sqlite3',  
        'NAME': BASE_DIR / 'db.sqlite3',  
    }  
}  
  
  
 Password validation  
 https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators  
  
AUTH_PASSWORD_VALIDATORS = [  
    {  
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',  
    },  
    {  
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',  
    },  
    {  
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',  
    },  
    {  
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',  
    },  
]  
  
  
Internationalization  
https://docs.djangoproject.com/en/4.1/topics/i18n/  
  
LANGUAGE_CODE = 'en-us'  
  
TIME_ZONE = 'UTC'  
  
USE_I18N = True  
  
USE_TZ = True  
  
  
 Static files (CSS, JavaScript, Images)  
 https://docs.djangoproject.com/en/4.1/howto/static-files/  
  
STATIC_URL = 'static/'  
  
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'  
  
STATIC_ROOT = BASE_DIR / 'staticfiles'   
  
 Default primary key field type  
 https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field  
  
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'  

And here's log from site and container : 253522-ff.png

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2022-10-24T07:15:22.94+00:00

    Hi anonymous user-5627, according to the logs, there was nothing wrong with your deployment. So, your 500 error is likely due to a bug in your Python code. Make sure your app runs locally fine. If it does, then the issue might be a misconfiguration in your settings.py, like where you set SECRET_KEY or ALLOWED_HOSTS. One way is look at the diagnostic logs when you attempt to navigate to your site and see what errors you're getting.

    ---
    EDIT 2022 Oct 27 Replace ALLOWED_HOST [.azurewebsites.net] with ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']] if 'WEBSITE_HOSTNAME' in os.environ else []

    What this does is pulls the host (i.e. app name) and adds it as an allowed host for your django app. When ALLOWED_HOSTS is [] the default is localhost which allows you to successfully run your app locally.

    1 person found this answer helpful.
    0 comments No comments

  2. Corentin 1 Reputation point
    2022-10-24T11:52:49.667+00:00

    Thanks for the info!

    EDIT : Thanks to the first answer i now know that azure is not the problem.

    Now i can't go further in investigating why it's not working without help.

    after 6 hours more on it still can't deploy this:
    here's my settings :

    from pathlib import Path  
      
    Build paths inside the project like this: BASE_DIR / 'subdir'.  
    BASE_DIR = Path(file).resolve().parent.parent  
      
    Quick-start development settings - unsuitable for production  
    See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/  
      
    SECRET_KEY = 'SECURITY WARNING: keep the secret key used in production secret!'  
      
    SECURITY WARNING: don't run with debug turned on in production!  
    DEBUG = False  
      
    ALLOWED_HOSTS = [  
    '.azurewebsites.net'  
    ]  
    Application definition  
      
    INSTALLED_APPS = [  
    'django.contrib.admin',  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.messages',  
    'django.contrib.staticfiles',  
    'azurecloud.apps.AzurecloudConfig'  
    ]  
      
    MIDDLEWARE = [  
    'django.middleware.security.SecurityMiddleware',  
    'whitenoise.middleware.WhiteNoiseMiddleware',  
    'django.contrib.sessions.middleware.SessionMiddleware',  
    'django.middleware.common.CommonMiddleware',  
    'django.middleware.csrf.CsrfViewMiddleware',  
    'django.contrib.auth.middleware.AuthenticationMiddleware',  
    'django.contrib.messages.middleware.MessageMiddleware',  
    'django.middleware.clickjacking.XFrameOptionsMiddleware',  
    ]  
      
    ROOT_URLCONF = 'azuredjango_project.urls'  
      
    TEMPLATES = [  
    {  
    'BACKEND': 'django.template.backends.django.DjangoTemplates',  
    'DIRS': [],  
    'APP_DIRS': True,  
    'OPTIONS': {  
    'context_processors': [  
    'django.template.context_processors.debug',  
    'django.template.context_processors.request',  
    'django.contrib.auth.context_processors.auth',  
    'django.contrib.messages.context_processors.messages',  
    ],  
    },  
    },  
    ]  
      
    WSGI_APPLICATION = 'azuredjango_project.wsgi.application'  
      
    Database  
    https://docs.djangoproject.com/en/4.1/ref/settings/#databases  
      
    DATABASES = {  
    'default': {  
    'ENGINE': 'django.db.backends.sqlite3',  
    'NAME': BASE_DIR / 'db.sqlite3',  
    }  
    }  
      
    Password validation  
    https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators  
      
    AUTH_PASSWORD_VALIDATORS = [  
    {  
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',  
    },  
    {  
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',  
    },  
    {  
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',  
    },  
    {  
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',  
    },  
    ]  
      
    Internationalization  
    https://docs.djangoproject.com/en/4.1/topics/i18n/  
      
    LANGUAGE_CODE = 'en-us'  
      
    TIME_ZONE = 'UTC'  
      
    USE_I18N = True  
      
    USE_TZ = True  
    

    Static files (CSS, JavaScript, Images)
    https://docs.djangoproject.com/en/4.1/howto/static-files/

    STATIC_URL = 'static/'

    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

    STATIC_ROOT = BASE_DIR / 'staticfiles'

    Default primary key field type
    https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field

    DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


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.