- Python Pyramid教程
- Python Pyramid - 主页
- Python Pyramid - 概述
- Pyramid - 环境设置
- Python Pyramid - Hello World
- Pyramid - 应用程序配置
- Python Pyramid - URL 路由
- Python Pyramid - 查看配置
- Python Pyramid - 路由前缀
- Python Pyramid - 模板
- Pyramid - HTML 表单模板
- Python Pyramid - 静态资源
- Python Pyramid - 请求对象
- Python Pyramid - 响应对象
- Python Pyramid - 会话
- Python Pyramid - 事件
- Python Pyramid - 消息闪烁
- Pyramid - 使用 SQLAlchemy
- Python Pyramid - Cookiecutter
- Python Pyramid - 创建项目
- Python Pyramid - 项目结构
- Python Pyramid - 包结构
- 手动创建项目
- 命令行Pyramid
- Python Pyramid - 测试
- Python Pyramid - 日志记录
- Python Pyramid - 安全
- Python Pyramid - 部署
- Python Pyramid有用资源
- Python Pyramid - 快速指南
- Python Pyramid - 有用的资源
- Python Pyramid - 讨论
Python Pyramid - 创建项目
假设 Pyramid 虚拟环境已启动并运行,并且其中安装了 Cookiecutter。创建 Cookiecutter 项目的最简单方法是按照以下命令使用预构建的启动模板 -
cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout 2.0-branch
下载模板并询问用户选择的项目名称 -
project_name [Pyramid Scaffold]: testproj repo_name [testproj]:
然后选择模板语言。
选择模板语言-
1 - jinja2 2 - chameleon 3 - mako Choose from 1, 2, 3 [1]: 1
由于我们熟悉 jinja2,因此选择 1。接下来,使用 SQLALchemy 作为后端。
Select backend: 1 - none 2 - sqlalchemy 3 - zodb Choose from 1, 2, 3 [1]: 2
在testproj文件夹内,创建以下文件结构 -
│ development.ini
│ MANIFEST.in
│ production.ini
│ pytest.ini
│ README.txt
│ setup.py
│ testing.ini
│
├───testproj
│ │ pshell.py
│ │ routes.py
│ │ __init__.py
│ │
│ ├───alembic
│ │ │ env.py
│ │ │ script.py.mako
│ │ │
│ │ └───versions
│ │ README.txt
│ │
│ ├───models
│ │ meta.py
│ │ mymodel.py
│ │ __init__.py
│ │
│ ├───scripts
│ │ initialize_db.py
│ │ __init__.py
│ │
│ ├───static
│ │ pyramid-16x16.png
│ │ pyramid.png
│ │ theme.css
│ │
│ ├───templates
│ │ 404.jinja2
│ │ layout.jinja2
│ │ mytemplate.jinja2
│ │
│ └───views
│ default.py
│ notfound.py
│ __init__.py
│
└───tests
conftest.py
test_functional.py
test_views.py
__init__.py
外部testproj文件夹有一个内部testproj包子文件夹和测试包。内部testproj 子文件夹是一个包含模型和脚本、子包以及静态和模板文件夹的包。
接下来,使用 Alembic 初始化和升级数据库。
# Generate your first revision. alembic -c development.ini revision --autogenerate -m "init" # Upgrade to that revision. alembic -c development.ini upgrade head
Alembic 是一个轻量级数据库迁移工具,可与 SQLAlchemy Database Toolkit for Python 一起使用。外部项目文件夹现在将显示testproj.sqlite数据库。
development.ini 文件提供数据库的默认数据。通过以下命令将其填充到数据库中。
initialize_testproj_db development.ini
Cookiecutter 实用程序还会在测试包中生成测试套件。它们基于PyTest包。继续看看测试是否通过。
Pytest ================ test session starts ====================== platform win32 -- Python 3.10.1, pytest-7.1.2, pluggy-1.0.0 rootdir: F:\pyram-env\testproj, configfile: pytest.ini, testpaths: testproj, tests plugins: cov-3.0.0 collected 5 items tests\test_functional.py .. [ 40%] tests\test_views.py ... [100%] =============== 5 passed, 20 warnings in 6.66s ===============
Cookiecutter 使用 Waitress 服务器。Pyramid 应用程序通过以下命令在本地主机的端口 6543 上提供服务 -
pserve development.ini Starting server in PID 67700. 2022-06-19 23:43:51,308 INFO [waitress:485][MainThread] Serving on http://[::1]:6543 2022-06-19 23:43:51,308 INFO [waitress:485][MainThread] Serving on http://127.0.0.1:6543
打开浏览器并在其中访问http://localhost:6543/ 。新创建的项目的主页将显示如下 -
调试工具栏
您可以在主页右上角找到较小的Pyramid徽标。单击它可打开一个新选项卡和一个调试工具栏,其中提供了有关项目的大量有用信息。
例如,历史记录标题下的 SQLAlchemy 选项卡显示 SQLAlchemy 查询,显示根据development.ini中的默认数据创建的模型的结构。
全局标题再次显示“内省”、“路由”等选项卡,如下所示。单击“路由”选项卡可查看应用程序配置中定义的路由及其匹配模式。
