使用Scaffolding构建一个Pyramid应用
在上述示例中,所有的工作都是在一个文件中(application.py)完成,虽然这是一种非常好的方式来展示如何使用Pyramid来压缩和简化构建MVC应用的过程,但这不总是最简单的方法。
与大多数流行框架类似,Pyramid能够使用scaffolding来快速创建一个复杂的项目目录结构。通过pcreate这个工具来使用scaffolding。Wikipedia上关于scaffolding的介绍为: Scaffolding is a technique supported by some model-view-controller frameworks, in which the programmer may write a specification that describes how the application database may be used. The compiler uses this specification to generate code that the application can use to create, read, update and delete database entries, effectively treating the template as a "scaffold" on which to build a more powerful application.
进入Pyramid主目录下,查看可用的scaffolding:
#cd pyramid_sites
#pcreate -l
Available scaffolds:
alchemy: Pyramid SQLAlchemy project using url dispatch
starter: Pyramid starter project
zodb: Pyramid ZODB project using traversal
注解:
1. alchemy能够使用SQL融合来创建一个项目
2. starter能够创建一个在应用实体之间非持续性的基本项目
3. zodb能够创建一个依靠ZODB来运行的项目
使用starter模板来创建第一个项目:
#pcreate -s starter first_project
#cd first_project
#ls
CHANGES.txt first_project MANIFEST.in README.txt
development.ini first_project.egg-info production.ini setup.py
在该目录下的文件大多是用于配置的,程序本身主要包含在以项目名字命名的一个子目录中
运行setup脚本来配置应用的开发环境:
#python setup.py develop
running develop
running egg_info
writing requirements to first_project.egg-info/requires.txt
writing first_project.egg-info/PKG-INFO
writing top-level names to first_project.egg-info/top_level.txt
writing dependency_links to first_project.egg-info/dependency_links.txt
writing entry points to first_project.egg-info/entry_points.txt
writing requirements to first_project.egg-info/requires.txt
writing first_project.egg-info/PKG-INFO
writing top-level names to first_project.egg-info/top_level.txt
writing dependency_links to first_project.egg-info/dependency_links.txt
writing entry points to first_project.egg-info/entry_points.txt
reading manifest file 'first_project.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
warning: no files found matching '*.cfg'
......
该命令将使用定义在development.ini中的可用参数来配置你的应用
最后运行你的项目:
#pserve development.ini
Starting server in PID 21251.
serving on http://0.0.0.0:6543
登录http://ip:6543 ,可查看到默认的应用界面
增加调试面板:Pyramid Debug Toolbar,
#vim development.ini
#在[app:main]选项中增加以下语句来使得所有连接到的主机都能够看到调试面板
debugtoolbar.hosts = 0.0.0.0/0
保存并退出,重启服务器可以看到调试面板在右手边
#pserve development.ini
——游响云停