Skip to content

Commit ee00c7b

Browse files
Merge pull request #226743 from pamelafox/postgres-tut
Updates to Python Flask/Django + PostgreSQL tutorial
2 parents 15ae502 + f6e5fbb commit ee00c7b

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed
-22.8 KB
Loading

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

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: 'Tutorial: Deploy a Python Django or Flask web app with PostgreSQL'
33
description: Create a Python Django or Flask web app with a PostgreSQL database and deploy it to Azure. The tutorial uses either the Django or Flask framework and the app is hosted on Azure App Service on Linux.
44
ms.devlang: python
55
ms.topic: tutorial
6-
ms.date: 10/07/2022
6+
ms.date: 02/28/2023
77
ms.custom: mvc, seodec18, seo-python-october2019, cli-validate, devx-track-python, devx-track-azurecli, devdivchpfy22, event-tier1-build-2022, vscode-azure-extension-update-completed
88
---
99

@@ -15,14 +15,14 @@ In this tutorial, you'll deploy a data-driven Python web app (**[Django](https:/
1515

1616
**To complete this tutorial, you'll need:**
1717

18-
* An Azure account with an active subscription exists. If you don't have an Azure account, you [can create one for free](https://azure.microsoft.com/free/python).
18+
* An Azure account with an active subscription. If you don't have an Azure account, you [can create one for free](https://azure.microsoft.com/free/python).
1919
* Knowledge of Python with Flask development or [Python with Django development](/training/paths/django-create-data-driven-websites/)
2020

2121
## Sample application
2222

2323
Sample Python applications using the Flask and Django framework are provided to help you follow along with this tutorial. To deploy them without running them locally, skip this part.
2424

25-
To run the application locally, make sure you have [Python 3.7 or higher](https://www.python.org/downloads/) and [PostgreSQL](https://www.postgresql.org/download/) install locally. Then, download or clone the app:
25+
To run the application locally, make sure you have [Python 3.7 or higher](https://www.python.org/downloads/) and [PostgreSQL](https://www.postgresql.org/download/) installed locally. Then, download or clone the app:
2626

2727
### [Flask](#tab/flask)
2828

@@ -124,9 +124,9 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
124124
1. *Resource Group* → Select **Create new** and use a name of **msdocs-python-postgres-tutorial**.
125125
1. *Region* → Any Azure region near you.
126126
1. *Name* → **msdocs-python-postgres-XYZ** where *XYZ* is any three random characters. This name must be unique across Azure.
127-
1. *Runtime stack* → **Python 3.9**.
127+
1. *Runtime stack* → **Python 3.10**.
128+
1. *Database* → **PostgreSQL - Flexible Server** is selected by default as the database engine. The server name and database name is also set by default to appropriate values.
128129
1. *Hosting plan* → **Basic**. When you're ready, you can [scale up](manage-scale-up.md) to a production pricing tier later.
129-
1. **PostgreSQL - Flexible Server** is selected by default as the database engine.
130130
1. Select **Review + create**.
131131
1. After validation completes, select **Create**.
132132
:::column-end:::
@@ -149,8 +149,6 @@ Sign in to the [Azure portal](https://portal.azure.com/) and follow these steps
149149
:::column-end:::
150150
:::row-end:::
151151

152-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
153-
154152
## 2. Verify connection settings
155153

156154
The creation wizard generated the connectivity variables for you already as [app settings](configure-common.md#configure-app-settings).
@@ -165,15 +163,18 @@ The creation wizard generated the connectivity variables for you already as [app
165163
:::row-end:::
166164
:::row:::
167165
:::column span="2":::
168-
**Step 2.** In the **Application settings** tab of the **Configuration** page, verify that `DBNAME`, `DBHOST`, `DBUSER`, and `DBPASS` are present. They'll be injected into the runtime environment as environment variables.
169-
App settings are a good way to keep connection secrets out of your code repository.
166+
**Step 2.** In the **Application settings** tab of the **Configuration** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` is present. That will be injected into the runtime environment as an environment variable.
167+
App settings are one way to keep connection secrets out of your code repository.
168+
When you're ready to move your secrets to a more secure location,
169+
here's an [article on storing in Azure Key Vault](/azure/key-vault/certificates/quick-create-python).
170170
:::column-end:::
171171
:::column:::
172172
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png" alt-text="A screenshot showing how to see the autogenerated connection string." lightbox="./media/tutorial-python-postgresql-app/azure-portal-get-connection-string-2.png":::
173173
:::column-end:::
174174
:::row-end:::
175175

176-
Having issues? Refer first to the [Troubleshooting guide](configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/DjangoCLITutorialHelp).
176+
Having issues? Check the [Troubleshooting guide](configure-language-python.md#troubleshooting).
177+
177178

178179
## 3. Deploy sample code
179180

@@ -226,6 +227,7 @@ In this step, you'll configure GitHub deployment using GitHub Actions. It's just
226227
1. In **Organization**, select your account.
227228
1. In **Repository**, select **msdocs-flask-postgresql-sample-app**.
228229
1. In **Branch**, select **main**.
230+
1. Keep the default option selected to **Add a workflow**.
229231
1. In the top menu, select **Save**. App Service commits a workflow file into the chosen GitHub repository, in the `.github/workflows` directory.
230232
:::column-end:::
231233
:::column:::
@@ -325,13 +327,14 @@ In this step, you'll configure GitHub deployment using GitHub Actions. It's just
325327

326328
-----
327329

328-
Having issues? Refer first to the [Troubleshooting guide](configure-language-python.md#troubleshooting), otherwise, [let us know](https://aka.ms/DjangoCLITutorialHelp).
330+
Having issues? Check the [Troubleshooting guide](configure-language-python.md#troubleshooting).
331+
329332

330333
## 4. Generate database schema
331334

332335
### [Flask](#tab/flask)
333336

334-
With the PostgreSQL database protected by the virtual network, the easiest way to run Run [Flask database migrations](https://flask-migrate.readthedocs.io/en/latest/) is in an SSH session with the App Service container.
337+
With the PostgreSQL database protected by the virtual network, the easiest way to run [Flask database migrations](https://flask-migrate.readthedocs.io/en/latest/) is in an SSH session with the App Service container.
335338

336339
:::row:::
337340
:::column span="2":::
@@ -380,8 +383,6 @@ With the PostgreSQL database protected by the virtual network, the easiest way t
380383
381384
-----
382385

383-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
384-
385386
## 5. Browse to the app
386387

387388
:::row:::
@@ -396,27 +397,25 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
396397
:::row-end:::
397398
:::row:::
398399
:::column span="2":::
399-
**Step 2.** Add a few tasks to the list.
400-
Congratulations, you're running a secure data-driven Flask app in Azure App Service, with connectivity to Azure Database for PostgreSQL.
400+
**Step 2.** Add a few restaurants to the list.
401+
Congratulations, you're running a web app in Azure App Service, with secure connectivity to Azure Database for PostgreSQL.
401402
:::column-end:::
402403
:::column:::
403404
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-browse-app-2.png" alt-text="A screenshot of the Flask web app with PostgreSQL running in Azure showing restaurants and restaurant reviews." lightbox="./media/tutorial-python-postgresql-app/azure-portal-browse-app-2.png":::
404405
:::column-end:::
405406
:::row-end:::
406407

407-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
408-
409408
## 6. Stream diagnostic logs
410409

411410
Azure App Service captures all messages output to the console to help you diagnose issues with your application. The sample app includes `print()` statements to demonstrate this capability as shown below.
412411

413412
### [Flask](#tab/flask)
414413

415-
:::code language="python" source="~/msdocs-django-postgresql-sample-app/restaurant_review/views.py" range="12-16" highlight="2":::
414+
:::code language="python" source="~/msdocs-flask-postgresql-sample-app/app.py" range="37-41" highlight="3":::
416415

417416
### [Django](#tab/django)
418417

419-
:::code language="python" source="~/msdocs-flask-postgresql-sample-app/app.py" range="39-43" highlight="3":::
418+
:::code language="python" source="~/msdocs-django-postgresql-sample-app/restaurant_review/views.py" range="12-16" highlight="2":::
420419

421420
-----
422421

@@ -425,6 +424,7 @@ Azure App Service captures all messages output to the console to help you diagno
425424
**Step 1.** In the App Service page:
426425
1. From the left menu, select **App Service logs**.
427426
1. Under **Application logging**, select **File System**.
427+
1. In the top menu, select **Save**.
428428
:::column-end:::
429429
:::column:::
430430
:::image type="content" source="./media/tutorial-python-postgresql-app/azure-portal-stream-diagnostic-logs-1.png" alt-text="A screenshot showing how to enable native logs in App Service in the Azure portal." lightbox="./media/tutorial-python-postgresql-app/azure-portal-stream-diagnostic-logs-1.png":::
@@ -439,7 +439,7 @@ Azure App Service captures all messages output to the console to help you diagno
439439
:::column-end:::
440440
:::row-end:::
441441

442-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
442+
Learn more about logging in Python apps in the series on [setting up Azure Monitor for your Python application](/azure/azure-monitor/app/opencensus-python]).
443443

444444
## 7. Clean up resources
445445

@@ -474,8 +474,6 @@ When you're finished, you can delete all of the resources from your Azure subscr
474474
:::column-end:::
475475
:::row-end:::
476476

477-
Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
478-
479477
## Frequently asked questions
480478

481479
- [How much does this setup cost?](#how-much-does-this-setup-cost)
@@ -487,10 +485,10 @@ Having issues? [Let us know](https://aka.ms/DjangoCLITutorialHelp).
487485

488486
#### How much does this setup cost?
489487

490-
Pricing for the create resources is as follows:
488+
Pricing for the created resources is as follows:
491489

492490
- The App Service plan is created in **Basic** tier and can be scaled up or down. See [App Service pricing](https://azure.microsoft.com/pricing/details/app-service/linux/).
493-
- The PostgreSQL flexible server is create in the lowest burstable tier **Standard_B1ms**, with the minimum storage size, which can be scaled up or down. See [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/).
491+
- The PostgreSQL flexible server is created in the lowest burstable tier **Standard_B1ms**, with the minimum storage size, which can be scaled up or down. See [Azure Database for PostgreSQL pricing](https://azure.microsoft.com/pricing/details/postgresql/flexible-server/).
494492
- The virtual network doesn't incur a charge unless you configure extra functionality, such as peering. See [Azure Virtual Network pricing](https://azure.microsoft.com/pricing/details/virtual-network/).
495493
- The private DNS zone incurs a small charge. See [Azure DNS pricing](https://azure.microsoft.com/pricing/details/dns/).
496494

@@ -502,7 +500,7 @@ Pricing for the create resources is as follows:
502500

503501
#### How does local app development work with GitHub Actions?
504502

505-
Take the autogenerated workflow file from App Service as an example, each `git push` kicks off a new build and deployment run. From a local clone of the GitHub repository, you make the desired updates push it to GitHub. For example:
503+
Using the autogenerated workflow file from App Service as an example, each `git push` kicks off a new build and deployment run. From a local clone of the GitHub repository, you make the desired updates and push to GitHub. For example:
506504

507505
```terminal
508506
git add .
@@ -533,11 +531,11 @@ For more information, see [Production settings for Django apps](configure-langua
533531

534532
#### I can't connect to the SSH session
535533

536-
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'DBNAME'`, it may mean that the environment variable is missing (you may have removed the app setting).
534+
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'AZURE_POSTGRESQL_CONNECTIONSTRING'`, it may mean that the environment variable is missing (you may have removed the app setting).
537535

538536
#### I get an error when running database migrations
539537

540-
If you encounter any errors related to connecting to the database, check if the app settings (`DBHOST`, `DBNAME`, `DBUSER`, and `DBPASS`) have been changed. Without those settings, the migrate command can't communicate with the database.
538+
If you encounter any errors related to connecting to the database, check if the app settings (`AZURE_POSTGRESQL_CONNECTIONSTRING`) have been changed. Without that connection string, the migrate command can't communicate with the database.
541539

542540
## Next steps
543541

0 commit comments

Comments
 (0)