Skip to content

Commit 6377d96

Browse files
committed
first pass
1 parent b0e2675 commit 6377d96

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed
Loading
Loading

articles/app-service/containers/tutorial-python-postgresql-app.md

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
---
2-
<<<<<<< HEAD
32
title: 'Tutorial: Build a Python Django web app with PostgreSQL in Azure App Service'
43
description: Learn how to run a PostgreSQL database-driven Python Django web app in Azure.
54
services: app-service\web
@@ -10,7 +9,7 @@ ms.service: app-service-web
109
ms.workload: web
1110
ms.devlang: python
1211
ms.topic: tutorial
13-
ms.date: 11/14/2019
12+
ms.date: 12/14/2019
1413
ms.author: cephalin
1514
ms.custom: [mvc, seodec18, seo-python-october2019]
1615
---
@@ -29,7 +28,7 @@ In this tutorial, you learn how to:
2928
> * View diagnostic logs
3029
> * Manage the web app in the Azure portal
3130
32-
You can follow the steps in this article on macOS, Linux, or Windows. The steps are similar in most cases, but differences aren't detailed in this tutorial. Most of the examples below use a `bash` terminal window on Linux.
31+
You can follow the steps in this article on macOS, Linux, or Windows. The steps are similar in most cases, although differences aren't detailed in this tutorial. Most of the examples below use a `bash` terminal window on Linux.
3332

3433
## Prerequisites
3534

@@ -42,18 +41,18 @@ Before you start this tutorial:
4241

4342
## Connect to your local PostgreSQL server and create a database
4443

45-
First, create and connect to a local PostgreSQL server and database.
44+
First, create and connect to a local PostgreSQL server and database:
4645

4746
In a local terminal window, run `psql` to connect to your local PostgreSQL server as the built-in `postgres` user.
4847

4948
```bash
5049
psql -U postgres
5150
```
5251

53-
If the connection isn't successful, make sure your PostgreSQL server is running, or start it if necessary by running a command like:
52+
If the connection isn't successful, make sure your PostgreSQL server is running, or restart it if necessary by running a command like:
5453

5554
```bash
56-
postgres -D /usr/local/pgsql/data
55+
pg_ctl.exe restart -D "<postgresql data directory>"
5756
```
5857

5958
In your *postgres* directory, create a new database called *pollsdb*, and set up a new database user named *manager* with password *supersecretpass*:
@@ -72,11 +71,11 @@ Type `\q` to exit the PostgreSQL client.
7271

7372
Next, set up and run the sample Python Django web app.
7473

75-
The sample repository contains the finished data-driven [Django](https://www.djangoproject.com/) polls app from [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this or another data-driven Python Django web app to a local or cloud PostgreSQL database back end, and deploy the app to Azure App Service.
74+
The [djangoapp](https://github.com/Azure-Samples/djangoapp) sample repository contains the finished data-driven [Django](https://www.djangoproject.com/) polls app you get from following [Writing your first Django app](https://docs.djangoproject.com/en/2.1/intro/tutorial01/) in the Django documentation. You can connect this, or any other data-driven Python Django web app, to a local or cloud PostgreSQL database back end, and deploy the app to Azure App Service.
7675

7776
### Clone the sample app
7877

79-
Open a terminal window, and run the following commands to clone the sample app repository and change to the new working directory:
78+
In a terminal window, run the following commands to clone the sample app repository and change to the new working directory:
8079

8180
```bash
8281
git clone https://github.com/Azure-Samples/djangoapp.git
@@ -97,9 +96,7 @@ py -3 -m venv venv
9796
venv\scripts\activate
9897
```
9998

100-
101-
102-
In the `venv` environment, run *env.sh* or *env.ps1* to set the environment variables that *azuresite/settings.py* uses for the database connection settings.
99+
In the `venv` environment, change to your *djangoapp* folder and run *env.sh* or *env.ps1* to set the environment variables *azuresite/settings.py* will use for the database connection settings.
103100

104101
```bash
105102
source ./env.sh
@@ -109,7 +106,7 @@ or
109106
.\env.ps1
110107
```
111108

112-
In the `venv` environment, install the required packages from *requirements.txt*, run [Django migrations](https://docs.djangoproject.com/en/2.1/topics/migrations/), and [create an admin user](https://docs.djangoproject.com/en/2.1/intro/tutorial02/#creating-an-admin-user):
109+
Install the required packages from *requirements.txt*, run [Django migrations](https://docs.djangoproject.com/en/2.1/topics/migrations/), and [create an admin user](https://docs.djangoproject.com/en/2.1/intro/tutorial02/#creating-an-admin-user):
113110

114111
```bash
115112
pip install -r requirements.txt
@@ -119,22 +116,22 @@ python manage.py createsuperuser
119116

120117
### Run the web app
121118

122-
Once the admin user is created, run the Django server.
119+
After you create the admin user, run the Django server.
123120

124121
```bash
125122
python manage.py runserver
126123
```
127124

128-
When the Django web app is fully loaded, you see something similar to the following message:
125+
When the Django web app is fully loaded, it returns something like the following message:
129126

130127
```bash
131128
Performing system checks...
132129

133130
System check identified no issues (0 silenced).
134-
October 26, 2018 - 10:54:59
131+
December 13, 2019 - 10:54:59
135132
Django version 2.1.2, using settings 'azuresite.settings'
136133
Starting development server at http://127.0.0.1:8000/
137-
Quit the server with CONTROL-C.
134+
Quit the server with CTRL-BREAK.
138135
```
139136

140137
Go to *http:\//localhost:8000* in a browser. You should see the message **No polls are available**.
@@ -211,7 +208,7 @@ az postgres server firewall-rule create --resource-group <resourcegroup-name> --
211208
> [!NOTE]
212209
> The preceding setting allows network connections from all IP addresses within the Azure network. For production use, try to configure the most restrictive firewall rules possible by [using only the outbound IP addresses your app uses](../overview-inbound-outbound-ips.md?toc=%2fazure%2fapp-service%2fcontainers%2ftoc.json#find-outbound-ips).
213210
214-
In the Cloud Shell, run the `firewall-rule-create` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
211+
Run the `firewall-rule-create` command again to allow access from your local computer. Replace *\<your-ip-address>* with [your local IPv4 IP address](https://www.whatsmyip.org/). Replace the *\<postgresql-name>* and *\<resourcegroup-name>* placeholders with your values.
215212

216213
```azurecli-interactive
217214
az postgres server firewall-rule create --resource-group <resourcegroup-name> --server-name <postgresql-name> --start-ip-address=<your-ip-address> --end-ip-address=<your-ip-address> --name AllowLocalClient
@@ -240,7 +237,7 @@ Type `\q` to exit the PostgreSQL client.
240237
241238
### Test app connectivity to the Azure Database for PostgreSQL production database
242239

243-
In the `venv` environment in your local terminal window, change the database environment variables, which you configure by running *env.sh* or *env.ps1*. Replace *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
240+
Edit your local *env.sh* or *env.ps1* file to change the environment variables to your cloud PostgreSQL database by replacing *\<postgresql-name>* with your Azure Database for PostgreSQL server name.
244241

245242
```bash
246243
export DBHOST="<postgresql-name>.postgres.database.azure.com"
@@ -256,6 +253,15 @@ $Env:DBNAME = "pollsdb"
256253
$Env:DBPASS = "supersecretpass"
257254
```
258255

256+
In the `venv` environment in your local terminal window, run *env.sh* or *env.ps1* again.
257+
```bash
258+
source ./env.sh
259+
```
260+
or
261+
```PowerShell
262+
.\env.ps1
263+
```
264+
259265
Run Django migration to the Azure database and create an admin user.
260266

261267
```bash
@@ -287,7 +293,7 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
287293

288294
1. Django validates the `HTTP_HOST` header in incoming requests. For your Django web app to work in App Service, you need to add the fully qualified domain name of the app to the allowed hosts.
289295

290-
Open *azuresite/settings.py* and change the `ALLOWED_HOSTS` line as follows.
296+
Edit *azuresite/settings.py* to change the `ALLOWED_HOSTS` line as follows.
291297

292298
```python
293299
ALLOWED_HOSTS = [os.environ['WEBSITE_SITE_NAME'] + '.azurewebsites.net', '127.0.0.1'] if 'WEBSITE_SITE_NAME' in os.environ else []
@@ -309,14 +315,13 @@ You need to change and add some settings in your *djangoapp/azuresite/settings.p
309315

310316
```python
311317
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
312-
313318
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
314319
```
315320

316321
For more information about configuring WhiteNoise, see the [WhiteNoise documentation](https://whitenoise.evans.io/en/stable/).
317322

318323
> [!IMPORTANT]
319-
> The database settings section already follows the security best practice of using environment variables. For the complete deployment recommendations, see [Django Documentation: deployment checklist](https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/).
324+
> The database settings section already follows the security best practice of using environment variables. For complete deployment recommendations, see [Django Documentation: deployment checklist](https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/).
320325
321326
Commit your changes into your fork of the repository:
322327

@@ -340,7 +345,7 @@ git commit -am "configure for App Service"
340345

341346
Earlier in the tutorial, you defined environment variables to connect to your PostgreSQL database.
342347

343-
In App Service, you set environment variables as _app settings_ by using the [`az webapp config appsettings set`](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command in Cloud Shell.
348+
In App Service, you set environment variables as *app settings* by using the [az webapp config appsettings set](/cli/azure/webapp/config/appsettings?view=azure-cli-latest#az-webapp-config-appsettings-set) command in Cloud Shell.
344349

345350
The following example specifies the database connection details as app settings.
346351

@@ -376,21 +381,17 @@ To https://<app-name>.scm.azurewebsites.net/<app-name>.git
376381
06b6df4..6520eea master -> master
377382
```
378383

379-
The App Service deployment server sees _requirements.txt_ in the repository root and runs Python package management automatically after `git push`.
384+
The App Service deployment server sees *requirements.txt* in the repository root and runs Python package management automatically after `git push`.
380385

381386
### Browse to the Azure app
382387

383-
Browse to the deployed app. It takes some time to start because the container needs to be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
384-
385-
```bash
386-
http://<app-name>.azurewebsites.net
387-
```
388+
Browse to the deployed app with URL *https:\//\<app-name>.azurewebsites.net*. It takes some time to start because the container needs to be downloaded and run when the app is requested for the first time. If the page times out or displays an error message, wait a few minutes and refresh the page.
388389

389390
You should see the poll question that you created earlier.
390391

391-
App Service detects a Django project in your repository by looking for a _wsgi.py_ in each subdirectory, which is created by `manage.py startproject` by default. When it finds the file, it loads the Django web app. For more information on how App Service loads Python apps, see [Configure built-in Python image](how-to-configure-python.md).
392+
App Service detects a Django project in your repository by looking for a *wsgi.py* file in each subdirectory, which `manage.py startproject` creates by default. When App Service finds the file, it loads the Django web app. For more information on how App Service loads Python apps, see [Configure built-in Python image](how-to-configure-python.md).
392393

393-
Go to `<app-name>.azurewebsites.net` and sign in using same admin user you created. If you like, try creating some more poll questions.
394+
Go to *https:\//\<app-name>.azurewebsites.net/admin* and sign in using the admin user you created. If you like, create some more poll questions.
394395

395396
![Run Python Django app in App Services in Azure](./media/tutorial-python-postgresql-app/run-python-django-app-in-azure.png)
396397

@@ -402,35 +403,24 @@ Go to `<app-name>.azurewebsites.net` and sign in using same admin user you creat
402403

403404
## Manage your app in the Azure portal
404405

405-
Go to the [Azure portal](https://portal.azure.com) to see the app you created.
406-
407-
From the left menu, select **App Services**, then select the name of your Azure app.
406+
In the [Azure portal](https://portal.azure.com) search for and select the app you created.
408407

409408
![Navigate to your Python Django app in the Azure portal](./media/tutorial-python-postgresql-app/navigate-to-django-app-in-app-services-in-the-azure-portal.png)
410409

411-
By default, the portal shows your app's **Overview** page. This page gives you a view of how your app is doing. Here, you can also perform basic management tasks like browse, stop, start, restart, and delete. The tabs on the left side of the page show the different configuration pages you can open.
410+
By default, the portal shows your app's **Overview** page. This page gives you a view of how your app is doing. Here, you can also perform basic management tasks like browse, stop, swap, restart, and delete. The tabs on the left side of the page show the different configuration pages you can open.
412411

413412
![Manage your Python Django app in the Overview page in the Azure portal](./media/tutorial-python-postgresql-app/manage-django-app-in-app-services-in-the-azure-portal.png)
414413

415414
[!INCLUDE [cli-samples-clean-up](../../../includes/cli-samples-clean-up.md)]
416415

417416
## Next steps
418417

419-
In this tutorial, you learned how to:
420-
421-
> [!div class="checklist"]
422-
> * Create a PostgreSQL database in Azure
423-
> * Connect a Python web app to PostgreSQL
424-
> * Deploy the Python web app to Azure
425-
> * View diagnostic logs
426-
> * Manage the Python web app in the Azure portal
427-
428-
Advance to the next tutorial to learn how to map a custom DNS name to your app.
418+
Go to the next tutorial to learn how to map a custom DNS name to your app:
429419

430420
> [!div class="nextstepaction"]
431421
> [Tutorial: Map custom DNS name to your app](../app-service-web-tutorial-custom-domain.md)
432422
433-
Or, check out other resources:
423+
Or check out other resources:
434424

435425
> [!div class="nextstepaction"]
436426
> [Configure Python app](how-to-configure-python.md)

0 commit comments

Comments
 (0)