Deploying Django Web App using Devops CI/CD onto Azure App Service

Viewed 1085

I'm trying to deploy simple django web ap to Azure App Service using CI/CD pipeline (the most basic one that is offered by Microsoft for app deployment- no changes from me). However I'm getting the following error:

2021-03-08T16:55:51.172914117Z   File "", line 219, in _call_with_frames_removed
2021-03-08T16:55:51.172918317Z   File "/home/site/wwwroot/deytabank_auth/wsgi.py", line 13, in 
2021-03-08T16:55:51.172923117Z     from django.core.wsgi import get_wsgi_application
2021-03-08T16:55:51.172927017Z ModuleNotFoundError: No module named 'django'

I checked other threads and tried doing all the things mentioned but it did not help, or I am missing something:

In wsgi.py I added:

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../licenses_api')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../deytabank_auth')

from django.core.wsgi import get_wsgi_application


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deytabank_auth.settings')

application = get_wsgi_application()

But still getting the same error, where django is not recognized. I can see that reuqirements.txt is being installed successfully and it has all the neccessary libraries there (including Django)

My CI/CD yaml file looks like this:

# Python to Linux Web App on Azure
# Build your Python project and deploy it to Azure as a Linux Web App.
# Change python version to one thats appropriate for your application.
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- develop

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: '***'

  # Web app name
  webAppName: 'DeytabankAuth'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Environment name
  environmentName: 'DeytabankAuth'

  # Project root folder. Point to the folder containing manage.py file.
  projectRoot: $(System.DefaultWorkingDirectory)

  # Python version: 3.7
  pythonVersion: '3.7'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(pythonVersion)'
      displayName: 'Use Python $(pythonVersion)'

    - script: |
        python -m venv antenv
        source antenv/bin/activate
        python -m pip install --upgrade pip
        pip install setup
        pip install -r requirements.txt
      workingDirectory: $(projectRoot)
      displayName: "Install requirements"

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(projectRoot)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      displayName: 'Upload package'
      artifact: drop

- stage: Deploy
  displayName: 'Deploy Web App'
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: DeploymentJob
    pool:
      vmImage: $(vmImageName)
    environment: $(environmentName)
    strategy:
      runOnce:
        deploy:
          steps:

          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
            displayName: 'Use Python version'

          - task: AzureWebApp@1
            displayName: 'Deploy Azure Web App : DeytabankAuth'
            inputs:
              azureSubscription: $(azureServiceConnectionId)
              appName: $(webAppName)
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

Maybe I need to configure something in the Azure App Service? But i am not sure exactly what.

2 Answers

I have met this issue before, and the problem might be your deployment method. Not sure which one you use, but the classic deployment center below is being deprecated, try use the new deployment center. enter image description here Checked your workflow with the one worked on my side, there is nothing different. So I will post the correct step worked on my side for you to refer.

  1. check your project locally to make sure it could run successfully.

  2. Create a new web app (this is to make sure no damage on your web app) and navigate to the Deployment center page. enter image description here

  3. Go to your GitHub and navigate to GitHub Action page to see the log. enter image description here

  4. Test your web app and check the file structure on KuDu site: https://{yourappname}.scm.azurewebsites.net/wwwroot/ enter image description here

You could test by click the browse button like what I did. enter image description here If you want to run command, go to this site: https://{yourappname}.scm.azurewebsites.net/DebugConsole enter image description here


By the way, I post this link if you need deploy using DevOps.

The possible reason for this question is that you don't have Django installed.

In the Microsoft-hosted agent ubuntu-latest, Django is not pre-installed. That is, you need to install it manually.

pip install Django==3.1.7

Click this document for detailed information about downloading Django.

Related