生成 Django 和 PostgreSQL 应用并部署到 Azure

本文将指导你如何与 GitHub Copilot 交互,以生成对 PostgreSQL 数据库执行 CRUD作的本地 Django Web 应用。 接下来,它将指导你如何与 GitHub Copilot for Azure 交互,将 Web 应用和数据库部署到 Azure 应用服务和 Azure PostgreSQL 灵活服务器(以及多个支持的 Azure 服务)。

你创建的特定应用程序是一个普通的联系人管理应用程序,它使用列表详细信息样式的体系结构功能 CRUD作。

注释

使用大型语言模型(LLM)生成应用程序可能会产生不一致的结果。 结果取决于 LLM 模型、说明等。 本指南的重点是帮助你了解如何获得更好的结果。 但是,每次浏览此示例时,都会获得(可能)截然不同的结果。

先决条件

重要

GitHub Copilot 是由 GitHub 管理的单独订阅。 有关 GitHub Copilot 订阅和支持的问题,请参阅 GitHub Copilot 计划入门

准备聊天会话

  1. 在 Visual Studio Code 中,使用标题栏中的“切换聊天”按钮,或选择 Ctrl+Alt+i 打开聊天窗口。 使用“新建聊天”图标创建新的聊天会话。

  2. 在聊天区域中,选择 Agent 模式。 撰写本文时, Claude Sonnet 4 产生最佳结果。 使用可用于生成代码的最佳模型。

验证配置

确保 CLI 工具和 Visual Studio Code 已更新、正确配置和正常运行,以提高结果。

  1. 在新聊天中,输入以下提示:

    
    I want to create a new Django website that stores data in PostgreSQL. Then, I'll want
    to deploy that new website to Azure. Do I have everything installed on my local 
    computer that you will need to perform these tasks?
    
    

    GitHub Copilot 将请求运行一系列命令行检查的权限,以确保你拥有这些工具以及安装这些工具的最新版本。

  2. 在终端中,使用命令 az --upgrade更新 Azure CLI。

  3. 在终端中,使用命令安装 Azure CLI 的服务连接器无密码扩展 az extension add --name serviceconnector-passwordless --upgrade

  4. 在 Visual Studio Code 中,将默认终端设置为 Git Bash。 转到“文件 > 首选项 > 设置”,然后在“搜索设置”中键入“默认配置文件:Windows”,然后选择“Git Bash”。 可能需要重启 Visual Studio Code 才能使此设置生效。

    注释

    使用 Git Bash 并不严格必要,但在编写本文时,它会产生最佳结果。

  5. 在 Visual Studio Code 中,使用 PostgreSQL for Visual Studio Code(预览版)扩展并导航到 contacts 数据库。

  6. 在 Visual Studio Code 中,使用 Azure 扩展并确保已登录到 Azure 帐户和订阅。 在主侧栏中打开 Azure 扩展时,应能够查看现有订阅和资源。

  7. 为新应用程序文件创建新文件夹,并将其作为工作区在 Visual Studio Code 中打开。

设置本地数据库

虽然 GitHub Copilot 能够执行开发人员通常执行的任何应用程序开发任务,但如果在较小的步骤中执行某些任务,你将获得最佳结果。 若要改进结果,请在使用 GitHub Copilot 之前创建数据库并设置身份验证和授权。

  1. 创建新的聊天,并使用以下提示:

    
    On my PostgreSQL server localhost, please create a new database named market.
    
    Then create a new user <db-username> with password `<password>` and give that 
    user full rights (create tables and other db objects, CRUD data) to the new 
    market database.
    
    Please do the work, and only prompt me when you are unable to do it yourself.
    
    

    <db-username>分别替换为<password>所需的数据库用户名和密码。

  2. 在 Windows 计算机上,建议的安全最佳做法是将数据库用户名和密码存储在本地文件中:

    %APPDATA%\postgresql\pgpass.conf

    这通常会解析硬盘驱动器上的以下位置:

    c:\Users\<username>\AppSettings\Roaming\postgresql\pgpass.conf

    替换为 <username> 你的 Windows 用户名。

    该文件应使用以下格式:

    localhost:5432:<database-name>:<database-user>:<password>
    

    这假定你在本地计算机上使用 PostgreSQL 实例,并且它托管在默认端口 (5432) 上。

    替换为<database-name>contacts和替换<db-username><password>上一步中使用的凭据。

    有关文件的详细信息 pgpass.conf ,请参阅 PostgreSQL 的文档

  3. 将文件的路径 pgpass.conf 添加到 PATH 环境变量中。

  4. 测试连接以确保它正常工作。 使用 psql CLI 通过以下命令对其进行测试:

    psql -h localhost -U <db-username> -d contacts
    

    替换为 <db-username> 文件中的数据库用户名段 pgpass.conf

    pgpass.conf如果未正确设置,你将看到提示你键入密码。

使用 GitHub Copilot 生成应用

首先,提供有关在本地计算机上生成和测试应用程序的说明和指导。

  1. 在 Visual Studio Code 中,使用标题栏中的“切换聊天”按钮打开聊天窗口。 使用“新建聊天”图标创建新的聊天会话。

  2. 在聊天区域中,选择 Agent 模式。 撰写本文时, Claude Sonnet 4 产生最佳结果。 使用可用于生成代码的最佳模型。

  3. 使用以下提示开始生成应用程序:

    
    I want you to create a simple Contact Manager application using Django and PostgreSQL. 
    
    This should be a CRUD application, so create web pages that display a list of 
    contacts, view details, add a new contact, edit or delete a contact. Each Contact 
    is comprised of a contact's Name, Address, and Phone number. Since this is a 
    Python / Django project please make sure to work inside of a virtual environment (venv). 
    I've already created a PostgreSQL database at `localhost` named `contacts`. There are 
    no tables yet. For local development in PostgreSQL, I'm using a `pgpass.conf` file 
    and I have tested that it works. Prefer Git Bash in the terminal. Beyond that, if there's 
    anything I need to do, please include instructions. But I want you to do as much as 
    you can on your own.
    
    

    提示符具有以下功能:

    • 要创建的应用程序的类型。 在这种情况下,联系人管理应用程序。
    • 要使用的技术。 在这种情况下,Django 和 PostgreSQL。
    • 要生成的站点体系结构。 在这种情况下,CRUD 样式应用程序包含一个页面,其中列出了所有联系人,并允许向下钻取到特定联系人。
    • 有关问题域的更多详细信息。 在这种情况下,可以提供希望应用程序管理的数据字段,包括联系人的姓名、地址和电话号码。
    • 有关数据库的特定说明。 在这种情况下,指示 GitHub Copilot 使用已创建的特定数据库、提供数据库的状态以及如何交互
    • 有关环境的特定说明。 在本例中,指示它使用 Git Bash。 你还告诉它,你希望工作在 Python 环境(venv)中执行,这是一种最佳做法。 GitHub Copilot 可能会自行选择这些选项,但表示它明确使该过程顺利进行。
    • 明确期望它尽可能多地自行工作。 否则,GitHub Copilot 可能会提供要接受的说明。
    • 指令/上下文的显式预期。 如果需要执行其他作,请通过提供说明和指导来设置需要它帮助的预期。

    重要

    当 GitHub Copilot 使用终端创建新的虚拟环境时,Visual Studio Code 会 venv 检测并显示一个对话框,询问是否要使用它。 忽略该对话框。 它消失了。 允许 GitHub Copilot 仅对此作使用终端。

    GitHub Copilot 使用内置终端和 Visual Studio Code 环境来:

    • 创建 Python 虚拟环境
    • 安装库和其他依赖项
    • 生成代码文件
    • 生成数据库表
    • 生成自述文件以获取进一步说明
    • 创建测试数据
    • 启动本地 Web 服务器
    • 测试网站(使用简单浏览器或 curl)

    由于 LLM 如何生成代码,因此每次使用的命令及其生成的命令都是不同的。

使用适用于 Azure 的 GitHub Copilot 部署到 Azure

在 GitHub Copilot 在本地生成站点后,你将创作一个提示,要求 GitHub Copilot 对站点进行更改,以准备部署,然后执行部署。 GitHub Copilot for Azure 扩展通过创建 Bicep 文件并使用 CLI 运行这些文件 azd 来处理此请求。

使用以下提示...可以复制到记事本并更改括号中的任何值,例如<resource-group-name><region-name>,然后复制并粘贴到 GitHub Copilot 聊天中:


Please help me deploy this Django app to Azure. 

First, create and use a Resource Group named "<resource-group-name>" in the "<region-name>" region.

Second, create an Azure App Service for the Django app in the new "<resource-group-name>" in the "<region-name>" region.

Third, create a new Azure Database for PostgreSQL flexible server named "<server-name>" in a resource group named "<resource-group-name>" in my subscription "<subscription-id>". Use my current account ("<account-id>") as the Microsoft Entra administrator. For the PostgreSQL "Administrator login", use "<azure-database-username>" and password "<azure-database-password>". Use a "development" class database configuration in the "<region-name>" region. Create a new database named "contact_manager_db" and migrate all tables and data from the local version of "contact_manager_db". The local database uses the username "<local-database-username>" and password "<local-database-password>". Add my IP address as a firewall rule, allow public access to this resource through the internet using a public IP address, and allow public access from any Azure service within Azure to this server.

Fourth, use Service Connector (and any other services you need to make this configuration work successfully) to connect the web app to the database. You may need to modify the application code to accommodate Service Connector. Also, please ensure a secure connection between the Azure App Service web site and the Azure PostgreSQL Flexible Server.

Please choose the least expensive options.  

If you are prompted for an environment, use the name "contacts-env". Configure my firewall to allow my IP address. Beyond that, if there's anything 
I need to do, please include instructions. But I want you to do as much as you can on your own.

Before you start: 

- Do you have any questions that need to be clarified? 
- Please create a plan for deployment in the form of a TODO list, and then update the TODO list as you progress. Do not start until I have a chance to review your plan and tell you to proceed.

提示符具有以下功能:

  • 要使用的特定服务。 在本例中,你将告知要使用 Azure 应用服务、Azure PostgreSQL 灵活服务器、服务连接器。 你还向它提供“执行任何其他需要执行的作”的说明,以确保其正常工作。
  • 特定服务选项。 在这种情况下,你表示希望为每个服务使用最昂贵的选项。
  • 提示可能后续步骤。 在这种情况下,建议有必要进行一些代码修改才能使用服务连接器。
  • 提前预测决策。 在这种情况下,你可以提供它所需的设置的答案,例如环境名称,azd
  • 明确期望它自己做尽可能多的工作。 否则,它可能会提供说明供你接受。
  • 指令/上下文的显式预期。 设置在要求你采取行动时需要帮助和指导的预期。
  • 询问是否需要任何澄清。 这通常会显示潜在问题,如边缘情况或说明不明确。
  • 请求具有 TODO 列表的计划。 让你确信适用于 Azure 的 GitHub Copilot 了解分配,并计划按预期执行该分配。

GitHub Copilot 使用内置终端和 Visual Studio Code 环境来:

  • 更新代码文件以适应服务连接器
  • 生成 Bicep 文件
  • azd运行 CLI
  • 测试部署
  • 如有必要,请使用日志或其他方式调试部署

与 GitHub Copilot 交互

在执行许多任务之前,GitHub Copilot 需要输入。 输入暂停是引导 GitHub Copilot 进行课程更正的机会,以防止错误或自定义首选项生成的输出。

虽然工作正常,但你可以观看并同意它使用按钮询问 Continue 的大多数问题。

重要

如果收到意外的结果,请使用新的聊天会话重启。

有时,需要提供输入。 系统提示输入时,有一些不同的时刻:

  • 用户凭据 - 如果终端中的当前作需要用户名或密码,
  • 决策时刻 - 有时,GitHub Copilot 会提供列表中的多个选项,并询问你喜欢的选项。
  • 命令面板 - 有时,GitHub Copilot 使用扩展的功能,并且选项将显示在命令面板中。 进行适当的选择后,GitHub Copilot 会继续作。
  • 交互式登录 - Azure CLI 和 azd CLI 需要进行身份验证,并启动多种身份验证机制之一。

测试和请求更改

GitHub Copilot 完成后,可能会认为站点已完成且功能正常。 但是,测试可能会发现问题或意外/不需要的应用功能。

使用尽可能详细地描述问题的提示。 例如,如果应用程序不起作用,请尽可能多地提供信息,包括确切的错误消息 和预期结果。

中断流

有时,你可能会注意到 GitHub Copilot 被困在尝试重复执行相同任务的循环中,或者它停滞在从未返回的进程中。 例如,在诊断网站问题时,GitHub Copilot 可能想要运行如下命令:

az webapp log tail

当 GitHub Copilot 卡住时,可以通过多种方式之一中断 GitHub Copilot:

  • Ctrl+c
  • 在聊天中使用暂停按钮
  • 结束聊天会话并启动新聊天

重要

结束聊天会话会销毁会话期间构建的所有上下文,这可能或可能不需要。

若要为刚刚发生的事情提供上下文,并将其推入可能的解决方案,可以在中断 GitHub Copilot 后立即添加提示,例如:

You were just getting the logs from Azure App Service but it did not return 
so you got stuck. Try to interrupt after a minute once you get what you need 
from the logs.

后续步骤