python网站模板下载_Python 的 Web 应用程序模板 - Visual Studio | Microsoft Docs

Python Web 应用程序项目模板Python web application project templates

01/28/2019

JoshuaPartlow.png?size=32

olprod.png?size=32

本文内容

Visual Studio 中的 Python 支持在 Bottle、Flask 和 Django 框架中通过项目模板和可配置为处理不同框架的调试启动程序开发 Web 项目。Python in Visual Studio supports developing web projects in Bottle, Flask, and Django frameworks through project templates and a debug launcher that can be configured to handle various frameworks. 这些模板包括 requirements.txt 文件来声明必需的依赖项 。These templates include a requirements.txt file to declare the necessary dependencies. 基于其中一个模板创建项目时,Visual Studio 会提示安装这些包(请参阅本文后面的安装项目要求)。When creating a project from one of these templates, Visual Studio prompts you to install those packages (see Install project requirements later in this article).

也可以使用其他框架(如 Pyramid)的通用“Web 项目” 模板。You can also use the generic Web Project template for other frameworks such as Pyramid. 在这种情况下,不会随模板一起安装框架。In this case, no frameworks are installed with the template. 而是将必要的包安装到用于项目的环境中(请参阅 Python 环境窗口 - 包选项卡)。Instead, install the necessary packages into the environment you're using for the project (see Python environments window - Package tab).

有关将 Python Web 应用部署到 Azure 的信息,请参阅发布到 Azure 应用服务。For information on deploying a Python web app to Azure, see Publish to Azure App Service.

使用项目模板Use a project template

使用“文件” > “新建” > 项目”从模板中创建项目。You create a project from a template using File > New > Project. 要查看 Web 项目的模板,请选择对话框左侧的“Python” > “Web” 。To see templates for web projects, select Python > Web on the left side of the dialog box. 然后选择你所选的模板,提供项目和解决方案名称,设置解决方案目录和 Git 存储库选项,然后选择“确定” 。Then select a template of your choice, providing names for the project and solution, set options for a solution directory and Git repository, and select OK.

projects-new-project-dialog-web.png?view=vs-2019

前面提到的通用“Web 项目”模板只提供了一个空的 Visual Studio 项目,只有一个 Python 项目,没有代码,也没有任何假设 。The generic Web Project template, mentioned earlier, provides only an empty Visual Studio project with no code and no assumptions other than being a Python project. 有关“Azure 云服务”模板的详细信息,请参阅 Python 的 Azure 云服务项目。For details on the Azure Cloud Service template, see Azure cloud service projects for Python.

所有其他模板都基于 Bottle、Flask 或 Django Web 框架,可以分为如以下各节所述的三个通用组。All the other templates are based on the Bottle, Flask, or Django web frameworks, and fall into three general groups as described in the following sections. 由其中任一模板创建的应用中的代码都足以在本地运行和调试应用。The apps created by any of these templates contain sufficient code to run and debug the app locally. 每个模板还提供必要的 WSGI 应用对象 (python.org),以用于生产 Web 服务器。Each one also provides the necessary WSGI app object (python.org) for use with production web servers.

空白组Blank group

所有“空白 Web 项目”模板都会创建一个项目,其中包含极少的样本代码以及 requirements.txt 文件中声明的必要依赖项。All Blank Web Project templates create a project with more or less minimal boilerplate code and the necessary dependencies declared in a requirements.txt file.

模板Template

描述Description

空白 Bottle Web 项目Blank Bottle Web Project

在 app.py 中生成最小的应用,其中包括 / 的主页和 /hello/ 页,它使用非常短的内嵌页模板回显 。Generates a minimal app in app.py with a home page for / and a /hello/ page that echoes using a very short inline page template.

空白 Django Web 项目Blank Django Web Project

使用核心 Django 网站结构生成 Django 项目,但没有 Django 应用。Generates a Django project with the core Django site structure but no Django apps.

空白 Flask Web 项目Blank Flask Web Project

生成包含Generates a minimal app with a single "Hello World!" / 的单个“Hello World!”页的最小应用。page for /. This app is similar to the result of following the detailed steps in Quickstart: Use Visual Studio to create your first Python web app.

Web 组Web group

所有“ Web 项目”模板都会创建一个具有相同设计的初学者 Web 应用,而与所选的框架无关。All Web Project templates create a starter web app with an identical design regardless of the chosen framework. 该应用包含“主页”、“关于”和“联系人”页面,以及使用 Bootstrap 的导航栏和响应式设计。The app has Home, About, and Contact pages, along with a nav bar and responsive design using Bootstrap. 每个应用都被适当地配置为提供静态文件(CSS、JavaScript 和字体),并使用适合框架的页面模板机制。Each app is appropriately configured to serve static files (CSS, JavaScript, and fonts), and uses a page template mechanism appropriate for the framework.

模板Template

描述Description

Bottle Web 项目Bottle Web Project

生成一个应用,其静态文件包含在 static 文件夹中,并通过 app.py 中的代码进行处理 。Generates an app whose static files are contained in the static folder and handled through code in app.py. 单个页面的路由包含在 routes.py 中,views 文件夹包含页面模板 。Routing for the individual pages is contained in routes.py, and the views folder contains the page templates.

Django Web 项目Django Web Project

生成一个 Django 项目和一个 Django 应用,其中包含三个页面、身份验证支持和一个 SQLite 数据库(但没有数据模型)。Generates a Django project and a Django app with three pages, authentication support, and a SQLite database (but no data models).

Flask Web 项目Flask Web Project

生成一个应用,其静态文件包含在 static 文件夹中 。Generates an app whose static files are contained in the static folder. views.py 中的代码处理路由,页面模板使用 templates 文件夹中包含的 Jinja 引擎 。Code in views.py handles routing, with page templates using the Jinja engine contained in the templates folder. runserver.py 文件提供启动代码 。The runserver.py file provides startup code.

Flask/Jade Web 项目Flask/Jade Web Project

生成与使用“Flask Web 项目”模板生成的相同的应用,但使用 Jade 模板引擎的 Jade 扩展 。Generates the same app as with the Flask Web Project template but using the Jade extension for the Jinja templating engine.

投票组Polls group

“投票 Web 项目”模板创建一个初学者 Web 应用,用户可以通过该应用对不同的投票问题进行投票。The Polls Web Project templates create a starter web app through which users can vote on different poll questions. 每个应用都基于“Web”项目模板的结构生成,从而使用数据库来管理投票和用户响应 。Each app builds upon the structure of the Web project templates to use a database to manage the polls and user responses. 这些应用包含相应的数据模型以及用于从 samples.json 文件加载投票的特殊应用页 (/seed) 。The apps include appropriate data models and a special app page (/seed) that loads polls from a samples.json file.

模板Template

描述Description

投票 Bottle Web 项目Polls Bottle Web Project

生成可以针对使用 REPOSITORY_NAME 环境变量配置的内存中数据库、MongoDB 或 Azure 表存储运行的应用。Generates an app that can run against an in-memory database, MongoDB, or Azure Table Storage, which is configured using the REPOSITORY_NAME environment variable. 数据模型和数据存储代码包含在 models 文件夹中,settings.py 文件包含用于确定使用哪个数据存储的代码 。The data models and data store code are contained in the models folder, and the settings.py file contains code to determine which data store is used.

投票 Django Web 项目Polls Django Web Project

生成一个 Django 项目和一个 Django 应用,其中包含三个页面和一个 SQLite 数据库。Generates a Django project and a Django app with three pages and a SQLite database. 加入对 Django 管理界面的自定义设置,以允许经过身份验证的管理员创建和管理投票。Includes customizations to the Django administrative interface to allow an authenticated administrator to create and manage polls.

投票 Flask Web 项目Polls Flask Web Project

生成可以针对使用 REPOSITORY_NAME 环境变量配置的内存中数据库、MongoDB 或 Azure 表存储运行的应用。Generates an app that can run against an in-memory database, MongoDB, or Azure Table Storage, which is configured using the REPOSITORY_NAME environment variable. 数据模型和数据存储代码包含在 models 文件夹中,settings.py 文件包含用于确定使用哪个数据存储的代码 。The data models and data store code are contained in the models folder, and the settings.py file contains code to determine which data store is used. 该应用对页面模板使用 Jinja 引擎。The app uses the Jinja engine for page templates.

投票 Flask/Jade Web 项目Polls Flask/Jade Web Project

生成与使用“投票 Flask Web 项目”模板生成的相同的应用,但使用 Jade 模板引擎的 Jade 扩展 。Generates the same app as with the Polls Flask Web Project template but using the Jade extension for the Jinja templating engine.

安装项目要求Install project requirements

从特定于框架的模板创建项目时,会出现一个对话框,有助于使用 pip 安装所需的包。When creating a project from a framework-specific template, a dialog appears to help you install the necessary packages using pip. 我们还建议对 Web 项目使用虚拟环境,以便发布网站时包含正确的依赖关项:We also recommend using a virtual environment for web projects so that the correct dependencies are included when you publish your web site:

template-web-requirements-txt-wizard.png?view=vs-2019

如果使用的是源代码管理,通常会忽略虚拟环境文件夹,因为该环境只能使用 requirements.txt 重新创建 。If you're using source control, you typically omit the virtual environment folder as that environment can be recreated using only requirements.txt. 排除文件夹的最佳方法是先在上面的提示中选择“我将自行安装” ,然后在创建虚拟环境之前禁用自动提交。The best way to exclude the folder is to first select the I will install them myself in the prompt shown above, then disable auto-commit before creating the virtual environment.

部署到 Microsoft Azure App Service 时,选择一个 Python 版本作为站点扩展并手动安装包。When deploying to Microsoft Azure App Service, select a version of Python as a site extension and manually install packages. 此外,因为 Azure 应用服务从 Visual Studio 部署时不会自动安装 requirements.txt 文件中的包,请遵照 aka.ms/PythonOnAppService 上的配置详细信息操作。Also, because Azure App Service does not automatically install packages from a requirements.txt file when deployed from Visual Studio, follow the configuration details on aka.ms/PythonOnAppService.

Microsoft Azure 云服务支持 requirements.txt 文件 。Microsoft Azure Cloud Services does support the requirements.txt file. 有关详细信息,请参阅 Azure 云服务项目。

调试Debugging

启动 Web 项目进行调试时,Visual Studio 会在随机端口上启动一个本地 Web 服务器,并打开默认浏览器浏览至该地址和端口。When a web project is started for debugging, Visual Studio starts a local web server on a random port and opens your default browser to that address and port. 若要指定其他选项,请右键单击项目,选择“属性” 和“Web 启动器” 选项卡:To specify additional options, right-click the project, select Properties, and select the Web Launcher tab:

template-web-launcher-properties.png?view=vs-2019

在“调试” 组中:In the Debug group:

搜索路径、脚本参数、解释器参数和解释器路径:这些选项与用于普通调试的相同。Search Paths, Script Arguments, Interpreter Arguments, and Interpreter Path: these options are the same as for normal debugging.

启动 URL:指定要在浏览器中打开的 URL 。Launch URL: specifies the URL that is opened in your browser. 默认为 localhost。It defaults to localhost.

端口号:URL 中未指定端口时使用的端口(默认情况下,Visual Studio 会自动选择一个)。Port Number: the port to use if none is specified in the URL (Visual Studio selects one automatically by default). 此设置下,能够替代 SERVER_PORT 环境变量的默认值,该变量由模板用来配置本地调试服务器侦听的端口。This setting allows you to override the default value of the SERVER_PORT environment variable, which is used by the templates to configure the port the local debug server listens on.

“Run Server 命令”和“Debug Server 命令”组(后者位于图像中所显示内容的下方)中的属性确定启动 Web 服务器的方式 。The properties in the Run Server Command and Debug Server Command groups (the latter is below what's shown in the image) determine how the web server is launched. 由于许多框架需要使用当前项目外的脚本,因此可在此处配置该脚本并将启动模块的名称作为参数进行传递。Because many frameworks require the use of a script outside of the current project, the script can be configured here and the name of the startup module can be passed as a parameter.

命令:可以是 Python 脚本(*.py 文件)、模块名称(例如 python.exe -m module_name)或一行代码(例如 python.exe -c "code")。Command: can be a Python script (*.py file), a module name (as in, python.exe -m module_name), or a single line of code (as in, python.exe -c "code"). 下拉列表中的值表明这些类型中有哪些适用。The value in the drop-down indicates which of these types is intended.

参数:这些参数会在命令后的命令行上传递 。Arguments: these arguments are passed on the command line following the command.

环境:指定环境变量的 = 对的新行分隔的列表。Environment: a newline-separated list of = pairs specifying environment variables. 这些变量在所有可能会修改环境的属性(例如端口号和搜索路径)后进行设定,因此可能会覆盖这些值。These variables are set after all properties that may modify the environment, such as the port number and search paths, and so may overwrite these values.

任何项目属性或环境变量都可以使用 MSBuild 语法进行指定,例如:$(StartupFile) --port $(SERVER_PORT)。Any project property or environment variable can be specified with MSBuild syntax, for example: $(StartupFile) --port $(SERVER_PORT).

$(StartupFile) 是启动文件的相对路径,{StartupModule} 是启动文件的可导入名称。$(StartupFile) is the relative path to the startup file and {StartupModule} is the importable name of the startup file. $(SERVER_HOST) 和 $(SERVER_PORT) 是普通的环境变量,由 “启动 URL”和 “端口号”属性自动设定或由 “环境”属性设定。$(SERVER_HOST) and $(SERVER_PORT) are normal environment variables that are set by the Launch URL and Port Number properties, automatically, or by the Environment property.

备注

“Run Server 命令”中的值通过“Debug” > “Start Server”命令或 Ctrl+F5 使用;“Debug Server 命令”组中的值通过“Debug” > “Start Debug Server”命令或 F5 使用。Values in Run Server Command are used with the Debug > Start Server command or Ctrl+F5; values in the Debug Server Command group are used with the Debug > Start Debug Server command or F5.

Bottle 示例配置Sample Bottle configuration

“Bottle Web 项目” 模板包括执行必要配置的 Boilerplate 代码。The Bottle Web Project template includes boilerplate code that does the necessary configuration. 导入的 Bottle 应用可能不包含此代码,但在这种情况下,以下设置将使用已安装的 bottle 模块启动应用:An imported bottle app may not include this code, however, in which case the following settings launch the app using the installed bottle module:

运行服务器命令组:Run Server Command group:

命令:bottle(模块)Command: bottle (module)

参数:--bind=%SERVER_HOST%:%SERVER_PORT% {StartupModule}:appArguments: --bind=%SERVER_HOST%:%SERVER_PORT% {StartupModule}:app

调试服务器命令组:Debug Server Command group:

命令:bottle(模块)Command: bottle (module)

参数:--debug --bind=%SERVER_HOST%:%SERVER_PORT% {StartupModule}:appArguments --debug --bind=%SERVER_HOST%:%SERVER_PORT% {StartupModule}:app

使用 Visual Studio 进行调试时,不建议使用 --reload 选项。The --reload option is not recommended when using Visual Studio for debugging.

Pyramid 示例配置Sample Pyramid configuration

Pyramid 应用当前最好使用 pcreate 命令行工具进行创建。Pyramid apps are currently best created using the pcreate command-line tool. 创建应用后,可使用“基于现有 Python 代码”模板将其导入。Once an app has been created, it can be imported using the From existing Python code template. 这样操作后,选择“通用 Web 项目”自定义来配置选项 。After doing so, select the Generic Web Project customization to configure the options. 这些设置假设将 Pyramid 安装到 ..\env 处的虚拟环境。These settings assume that Pyramid is installed into a virtual environment at ..\env.

调试组:Debug group:

服务器端口:6543(或 .ini 文件中配置的任何内容 )Server Port: 6543 (or whatever is configured in the .ini files)

运行服务器命令组:Run Server Command group:

命令:..\env\scripts\pserve-script.py(脚本)Command: ..\env\scripts\pserve-script.py (script)

参数:Production.iniArguments: Production.ini

调试服务器命令组:Debug Server Command group:

命令:..\env\scripts\pserve-script.py(脚本)Command: ..\env\scripts\pserve-script.py (script)

参数:Development.iniArguments: Development.ini

提示

你可能需要配置项目的“工作目录” 属性,因为 Pyramid 应用通常是项目根目录下的一个文件夹。You likely need to configure the Working Directory property of your project because Pyramid apps are typically one folder below the project root.

其他配置Other configurations

如果有针对另一个要共享的框架的设置,或者要为另一个框架请求设置,则在 GitHub 上提出问题。If you have settings for another framework that you would like to share, or if you'd like to request settings for another framework, open an issue on GitHub.

将项目转换为 Azure 云服务Convert a project to Azure Cloud Service

“转换为 Microsoft Azure 云服务项目”命令(见下图)会将云服务项目添加到解决方案 。The Convert to Microsoft Azure Cloud Service Project command (image below) adds a cloud service project to your solution. 此项目包括要使用的虚拟机和服务的部署设置和配置。This project includes the deployment settings and configuration for the virtual machines and services to be used. 使用云项目上的“发布”命令部署到云服务;Python 项目上的“发布”命令仍会部署到网站 。Use the Publish command on the cloud project to deploy to Cloud Services; the Publish command on the Python project still deploys to Web Sites. 有关详细信息,请参阅 Azure 云服务项目。

template-web-convert-menu.png?view=vs-2019

请参阅See also

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值