# Django Deployment in Azure

## Steps to deploy an HTTP API in Azure functions

### Create function

**Install dependencies**

Before you can get started, you should install Node.js which includes npm.

* Run the following command to install the Core Tools package:

```
npm install -g azure-functions-core-tools@4 --unsafe-perm true
```

**Create an Azure Functions project**

In the terminal window or from a command prompt, navigate to an empty folder for your project, and run the following command:

```
func init
```

You will also be prompted to choose a runtime for the project. Select .

**Create a function**

To create a function, run the following command:

```
func new
```

This will prompt you to choose a template for your function. We recommend HTTP trigger for getting started.

**Run your function project locally**

Run the following command to start your function app:

```
func start
```

The runtime will output a URL for any HTTP functions, which can be copied and run in your browser's address bar.

To stop debugging, use Ctrl-C in the terminal.

**Deploy your code to Azure**

To publish your Functions project into Azure, enter the following command:

```
func azure functionapp publish django-azure-http
```

You may be prompted to sign into Azure (if not logged in already).

#### FAQS

`Value of AzureWebJobsStorage app setting is invalid`

Possible Issues and respective solutions:

* AzureWebJobsStorage env var not cofigured for the app, set the env var in the azure console to fix the issue.
* AzureWebJobsStorage value set in the env vars is invalid, check once if it is in the below format:

```
DefaultEndpointsProtocol=https;AccountName=****;AccountKey=*******;EndpointSuffix=core.windows.net
```

### Django App Deployment

**Method 1** (with serverless)

* create a file `azure_handler.py` at project root level with below content.

```py
# azure_handler.py

import azure.functions as func
from <your_project>.wsgi import application

def handler(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.WsgiMiddleware(application).handle(req, context)

```

* create a file `serverless.yml` at project root level with below content.

```yml
# serverless.yml

service: django-app-deployment

useDotenv: true

provider:
  name: azure
  region: EAST US
  runtime: python3.8
  prefix: ""
  stage: ${opt:stage, 'dev'}
  environment:
    DJANGO_SETTINGS_MODULE: ${self:custom.config.DJANGO_SETTINGS_MODULE}
    AZ_STORAGE_ACCOUNT_NAME: ${self:custom.config.AZ_STORAGE_ACCOUNT_NAME}
    AZ_STORAGE_CONTAINER: ${self:custom.config.AZ_STORAGE_CONTAINER}
    AZ_STORAGE_KEY: ${self:custom.config.AZ_STORAGE_KEY}
    AZ_DB_ENGINE: ${self:custom.config.AZ_DB_ENGINE}
    AZ_DB_HOSTNAME: ${self:custom.config.AZ_DB_HOSTNAME}
    AZ_DB_PORT: ${self:custom.config.AZ_DB_PORT}
    AZ_DB_USERNAME: ${self:custom.config.AZ_DB_PASSWORD}
    AZ_DB_PASSWORD: ${self:custom.config.AZ_DB_PASSWORD}
    AZ_DB_NAME: ${self:custom.config.AZ_DB_NAME}

plugins:
  - serverless-azure-functions

custom:
  config:
    DJANGO_SETTINGS_MODULE: ${env:DJANGO_SETTINGS_MODULE}
    AZ_STORAGE_ACCOUNT_NAME: ${env:AZ_STORAGE_ACCOUNT_NAME}
    AZ_STORAGE_CONTAINER: ${env:AZ_STORAGE_CONTAINER}
    AZ_STORAGE_KEY: ${env:AZ_STORAGE_KEY}
    AZ_DB_ENGINE: ${env:AZ_DB_ENGINE, 'django.db.backends.mysql'}
    AZ_DB_HOSTNAME: ${env:AZ_DB_HOSTNAME}
    AZ_DB_PORT: ${env:AZ_DB_PORT}
    AZ_DB_USERNAME: ${env:AZ_DB_USERNAME}
    AZ_DB_PASSWORD: ${env:AZ_DB_PASSWORD}
    AZ_DB_NAME: ${env:AZ_DB_NAME}

package:
  patterns:
    - '!.venv/**'
    - '!__pycache__/**'
    - '!.vscode/**'
    - '!.idea/**'

functions:
  usersCreate:
    handler: azure_handler.handler
    events:
      - http: true
        authLevel: anonymous
        name: req
        methods:
          - post
          - get
        route: "{*route}"
```

* create a dir inside your django project let say `azure_app`.
* create a file `azure_handler.py` with below content.

```py
import azure.functions as func
from <your_project>.wsgi import application

def handler(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return func.WsgiMiddleware(application).handle(req, context)

```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.techbytes.app/devops-docs/deployments/django-deployment-in-azure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
