You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## Create an Azure Database for PostgreSQL - Flexible Server
98
+
98
99
Create a flexible server with the [az postgreSQL flexible-server create](/cli/azure/postgres/flexible-server#az-postgres-flexible-server-create) command. The following command creates a server using service defaults and values from your Azure CLI's local context:
99
100
100
101
```azurecli-interactive
@@ -107,13 +108,11 @@ The server created has the below attributes:
107
108
- Using public-access argument allow you to create a server with public access to any client with correct username and password.
108
109
- Since the command is using local context it will create the server in the resource group ```django-project``` and in the region ```eastus```.
109
110
110
-
111
111
## Build your Django docker image
112
112
113
113
Create a new [Django application](https://docs.djangoproject.com/en/3.1/intro/) or use your existing Django project. Make sure your code is in this folder structure.
114
114
115
-
116
-
```
115
+
```python
117
116
└───my-djangoapp
118
117
└───views.py
119
118
└───models.py
@@ -130,8 +129,8 @@ Create a new [Django application](https://docs.djangoproject.com/en/3.1/intro/)
130
129
└─── Dockerfile
131
130
└─── requirements.txt
132
131
└─── manage.py
133
-
134
132
```
133
+
135
134
Update ```ALLOWED_HOSTS``` in ```settings.py``` to make sure the Django application uses the external IP that gets assigned to kubernetes app.
136
135
137
136
```python
@@ -171,42 +170,35 @@ Create a new file named ```Dockerfile``` and copy the code snippet below. This D
171
170
```docker
172
171
# Use the official Python image from the Docker Hub
Make sure you're in the directory ```my-django-app``` in a terminal using the ```cd``` command. Run the following command to build your bulletin board image:
205
199
206
-
```bash
207
-
200
+
```bash
208
201
docker build --tag myblog:latest .
209
-
210
202
```
211
203
212
204
Deploy your image to [Docker hub](https://docs.docker.com/get-started/part3/#create-a-docker-hub-repository-and-push-your-image) or [Azure Container registry](../../container-registry/container-registry-get-started-azure-cli.md).
@@ -217,7 +209,6 @@ Deploy your image to [Docker hub](https://docs.docker.com/get-started/part3/#cre
217
209
>```azurecli-interactive
218
210
>az aks update -n djangoappcluster -g django-project --attach-acr <your-acr-name>
219
211
> ```
220
-
>
221
212
222
213
## Create Kubernetes manifest file
223
214
@@ -327,13 +318,14 @@ Now open a web browser to the external IP address of your service (http://\<serv
327
318
328
319
## Run database migrations
329
320
330
-
For any django application, you would need to run database migration or collect static files. You can run these django shell commands using ```$ kubectl exec <pod-name> -- [COMMAND]```. Before running the command you need to find the pod name using ```kubectl get pods```.
321
+
For any django application, you would need to run database migration or collect static files. You can run these django shell commands using `$ kubectl exec <pod-name> -- [COMMAND]`. Before running the command you need to find the pod name using `kubectl get pods`.
In this tutorial you will learn how to deploy a Django application in Azure using App Services and Azure Database for PostgreSQL - Flexible Server in a virtual network.
19
+
In this tutorial you'll learn how to deploy a Django application in Azure using App Services and Azure Database for PostgreSQL - Flexible Server in a virtual network.
20
20
21
21
## Prerequisites
22
22
23
23
If you don't have an Azure subscription, create a [free](https://azure.microsoft.com/free/) account before you begin.
24
24
25
25
This article requires that you're running the Azure CLI version 2.0 or later locally. To see the version installed, run the `az --version` command. If you need to install or upgrade, see [Install Azure CLI](/cli/azure/install-azure-cli).
26
26
27
-
You'll need to login to your account using the [az login](/cli/azure/authenticate-azure-cli) command. Note the **id** property from the command output for the corresponding subscription name.
27
+
You'll need to log in to your account using the [az login](/cli/azure/authenticate-azure-cli) command. Note the **id** property from the command output for the corresponding subscription name.
28
28
29
29
```azurecli
30
30
az login
@@ -35,28 +35,25 @@ If you have multiple subscriptions, choose the appropriate subscription in which
az postgres flexible-server create --resource-group myresourcegroup --location westus2
86
-
87
82
```
88
83
89
84
This command performs the following actions, which may take a few minutes:
90
85
91
86
- Create the resource group if it doesn't already exist.
92
-
- Generates a server name if it is not provided.
87
+
- Generates a server name if it isn't provided.
93
88
- Create a new virtual network for your new postgreSQL server. **Make a note of virtual network name and subnet name** created for your server since you need to add the web app to the same virtual network.
94
-
- Creates admin username, password for your server if not provided. **Make a note of the username and password** to use in the next step.
89
+
- Creates admin username, password for your server if not provided. **Make a note of the username and password** to use in the next step.
95
90
- Create a database ```postgres``` that can be used for development. You can run [**psql** to connect to the database](quickstart-create-server-portal.md#connect-to-the-postgresql-database-using-psql) to create a different database.
96
91
97
92
> [!NOTE]
98
-
> Make a note of your password that will be generate for you if not provided. If you forget the password you would have to reset the password using ``` az postgres flexible-server update``` command
99
-
93
+
> Make a note of your password that will be generate for you if not provided. If you forget the password you would have to reset the password using `az postgres flexible-server update` command
100
94
101
95
## Deploy the code to Azure App Service
102
96
103
97
In this section, you create app host in App Service app, connect this app to the Postgres database, then deploy your code to that host.
104
98
105
-
106
99
### Create the App Service web app in a virtual network
107
100
108
101
In the terminal, make sure you're in the repository root (`djangoapp`) that contains the app code.
109
102
110
103
Create an App Service app (the host process) with the [`az webapp up`](/cli/azure/webapp#az-webapp-up) command:
az webapp config appsettings set --settings DJANGO_ENV="production" DBHOST="<postgres-server-name>.postgres.database.azure.com" DBNAME="postgres" DBUSER="<username>" DBPASS="<password>"
136
121
```
137
122
- For the `--location` argument, use the same location as you did for the database in the previous section.
@@ -141,7 +126,7 @@ az webapp config appsettings set --settings DJANGO_ENV="production" DBHOST="<pos
141
126
- Enable default logging for the app, if not already enabled.
142
127
- Upload the repository using ZIP deployment with build automation enabled.
143
128
-**az webapp vnet-integration** command adds the web app in the same virtual network as the postgres server.
144
-
- The app code expects to find database information in a number of environment variables. To set environment variables in App Service, you create "app settings" with the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings#az-webapp-config-appsettings-set) command.
129
+
- The app code expects to find database information in many environment variables. To set environment variables in App Service, you create "app settings" with the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings#az-webapp-config-appsettings-set) command.
145
130
146
131
> [!TIP]
147
132
> Many Azure CLI commands cache common parameters, such as the name of the resource group and App Service plan, into the file *.azure/config*. As a result, you don't need to specify all the same parameter with later commands. For example, to redeploy the app after making changes, you can just run `az webapp up` again without any parameters.
@@ -152,7 +137,7 @@ Django database migrations ensure that the schema in the PostgreSQL on Azure dat
152
137
153
138
1. Open an SSH session in the browser by navigating to *https://\<app-name>.scm.azurewebsites.net/webssh/host* and sign in with your Azure account credentials (not the database server credentials).
154
139
155
-
1. In the SSH session, run the following commands (you can paste commands using **Ctrl**+**Shift**+**V**):
140
+
2. In the SSH session, run the following commands (you can paste commands using **Ctrl**+**Shift**+**V**):
156
141
157
142
```bash
158
143
cd site/wwwroot
@@ -167,15 +152,15 @@ Django database migrations ensure that the schema in the PostgreSQL on Azure dat
167
152
python manage.py createsuperuser
168
153
```
169
154
170
-
1. The `createsuperuser`command prompts you for superuser credentials. For the purposes of this tutorial, use the default username `root`, press **Enter**for the email address to leave it blank, and enter `postgres1`for the password.
155
+
3. The `createsuperuser`command prompts you for superuser credentials. For the purposes of this tutorial, use the default username `root`, press **Enter**for the email address to leave it blank, and enter `postgres1`for the password.
171
156
172
157
### Create a poll question in the app
173
158
174
-
1. In a browser, open the URL *http:\//\<app-name>.azurewebsites.net*. The app should display the message "No polls are available" because there are no specific polls yet in the database.
159
+
4. In a browser, open the URL *http:\//\<app-name>.azurewebsites.net*. The app should display the message "No polls are available" because there are no specific polls yet in the database.
175
160
176
-
1. Browse to *http:\//\<app-name>.azurewebsites.net/admin*. Sign in using superuser credentials from the previous section (`root` and `postgres1`). Under **Polls**, select**Add** next to **Questions** and create a poll question with some choices.
161
+
5. Browse to *http:\//\<app-name>.azurewebsites.net/admin*. Sign in using superuser credentials from the previous section (`root` and `postgres1`). Under **Polls**, select**Add** next to **Questions** and create a poll question with some choices.
177
162
178
-
1. Browse again to *http:\//\<app-name>.azurewebsites.net/* to confirm that the questions are now presented to the user. Answer questions however you like to generate some data in the database.
163
+
6. Browse again to *http:\//\<app-name>.azurewebsites.net/* to confirm that the questions are now presented to the user. Answer questions however you like to generate some data in the database.
179
164
180
165
**Congratulations!** You're running a Python Django web app in Azure App Service for Linux, with an active Postgres database.
181
166
@@ -193,25 +178,20 @@ In a terminal window, run the following commands. Be sure to follow the prompts
Once the web app is fully loaded, the Django development server provides the local app URL in the message, "Starting development server at http://127.0.0.1:8000/. Quit the server with CTRL-BREAK".
@@ -222,30 +202,27 @@ Test the app locally with the following steps:
222
202
223
203
1. Go to *http:\//localhost:8000* in a browser, which should display the message "No polls are available".
224
204
225
-
1. Go to *http:\//localhost:8000/admin* and sign in using the admin user you created previously. Under **Polls**, again select **Add** next to **Questions** and create a poll question with some choices.
205
+
2. Go to *http:\//localhost:8000/admin* and sign in using the admin user you created previously. Under **Polls**, again select **Add** next to **Questions** and create a poll question with some choices.
226
206
227
-
1. Go to *http:\//localhost:8000* again and answer the question to test the app.
207
+
3. Go to *http:\//localhost:8000* again and answer the question to test the app.
228
208
229
-
1. Stop the Django server by pressing **Ctrl**+**C**.
209
+
4. Stop the Django server by pressing **Ctrl**+**C**.
230
210
231
211
When running locally, the app is using a local Sqlite3 database and doesn't interfere with your production database. You can also use a local PostgreSQL database, if desired, to better simulate your production environment.
232
212
233
-
234
-
235
213
### Update the app
236
214
237
215
In `polls/models.py`, locate the line that begins with `choice_text` and change the `max_length` parameter to 100:
238
216
239
217
```python
240
218
# Find this lie of code and set max_length to 100 instead of 200
Because you changed the data model, create a new Django migration and migrate the database:
247
224
248
-
```
225
+
```python
249
226
python manage.py makemigrations
250
227
python manage.py migrate
251
228
```
@@ -265,8 +242,6 @@ az webapp up
265
242
266
243
This command uses the parameters cached in the *.azure/config* file. Because App Service detects that the app already exists, it just redeploys the code.
267
244
268
-
269
-
270
245
### Rerun migrations in Azure
271
246
272
247
Because you made changes to the data model, you need to rerun database migrations in App Service.
@@ -278,11 +253,9 @@ cd site/wwwroot
278
253
279
254
# Activate default virtual environment in App Service container
@@ -304,7 +277,6 @@ By default, the portal shows your app's **Overview** page, which provides a gene
304
277
305
278
:::image type="content" source="./media/tutorial-django-app-service-postgres/manage-django-app-in-app-services-in-the-azure-portal.png" alt-text="Manage your Python Django app in the Overview page in the Azure portal":::
306
279
307
-
308
280
## Clean up resources
309
281
310
282
If you'd like to keep the app or continue to the next tutorial, skip ahead to [Next steps](#next-steps). Otherwise, to avoid incurring ongoing charges you can delete the resource group create for this tutorial:
0 commit comments