• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python Web框架Pylons中使用MongoDB的例子

Python Web框架Pylons中使用MongoDB的例子

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了pylons,shrines and pylons,nacelles/pylons,pylons是什么意思,python mongodb等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

Pylons 经过漫长的开发,终于放出了 1.0 版本。对于正规的产品开发来说,1.0 版本的意义很大,这表明 Pylons 的 API 终于稳定下来了。

Pylons 虽是山寨 Rails 而生,但作为一个纯 Python 的 Web 框架,它有一个鲜明的特点:可定制性强。框架每一层都没重新发明轮子,而是尽量整合现有的 Python 库。在 MVC 的 Model 层,Pylons 默认支持 SQLAlchemy。现在 NoSQL 很火 MongoDB 很热。在 Pylons 中应用 MongoDB 也很简单。下面是一个简单的示例。

在 PROJECT/model/__init__.py 中定义 MongoDB 初始化函数和映射对象:

from ming import schema
from ming.orm import MappedClass
from ming.orm import FieldProperty, ForeignIdProperty, RelationProperty
from ming.orm import ThreadLocalORMSession

session = None

def init_single_model(model_class):
    model_class.__mongometa__.session = session

class Page(MappedClass):
    class __mongometa__:
        session = session
        name = 'pages'

    _id = FieldProperty(schema.ObjectId)
    title = FieldProperty(str)
    content = FieldProperty(str)

def init_model(engine):
    global session
    session = ThreadLocalORMSession(doc_session=Session(engine))
    init_single_model(Page)
    MappedClass.compile_all()
</div>

在 PROJECT/config/environment.py 中进行初始化:

def load_environment(global_conf, app_conf):

    ...

    # Create the Mako TemplateLookup, with the default auto-escaping
    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=paths['templates'],
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the mongodb database engine
    init_model(DataStore(config['database.uri']))

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    return config
</div>

最后在 development.ini 中加入 MongoDB 的配置项:

如果需要在程序安装时初始化一些数据, 可以在 PROJECT/websetup.py 中加入

import pylons.test

from .config.environment import load_environment
from . import model

log = logging.getLogger(__name__)

def setup_app(command, conf, vars):
    """Place any commands to setup wukong here"""
    # Don't reload the app if it was loaded under the testing environment
    if not pylons.test.pylonsapp:
        load_environment(conf.global_conf, conf.local_conf)

        log.info("Adding demo data.")
        page = model.Page(title='demo', content='This is for demo.')
        model.session.flush()
        log.info("Successfully set up.")
</div>

这里使用了 Ming 库来连接 MongoDB 并做简单的 ORM。Ming 库是对 PyMongo 的 ORM 包装库。它是 SourceForge 用 TurboGears 和 MongoDB 对网站进行重构的副产物。使用起来有点象 SQLAlchemy ORM 。在上面的示例中,也可以把 Ming 替换成 MongoKit 或其它 MongoDB 的 ORM 库,甚至直接用 PyMongo 也无不可。
有种感觉,MongoDB 会火。

</div>

您可能想查找下面的文章:

  • Python Web框架Pylons中使用MongoDB的例子
  • Python Web框架Pylons中使用MongoDB的例子

相关文章

  • Python输出9*9乘法表的方法
  • 连接Python程序与MySQL的教程
  • 在Django的模型中执行原始SQL查询的方法
  • Python中super的用法实例
  • python访问mysql数据库的实现方法(2则示例)
  • 学习python的几条建议分享
  • python基础教程之自定义函数介绍
  • python基于xml parse实现解析cdatasection数据
  • Python中编写ORM框架的入门指引
  • python在windows下实现ping操作并接收返回信息的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • RC4文件加密的python实现方法
    • 对于Python中RawString的理解介绍
    • python安装mysql-python简明笔记(ubuntu环境)
    • Python实现简单多线程任务队列
    • python实现登陆知乎获得个人收藏并保存为word文件
    • 在Python中使用第三方模块的教程
    • python利用matplotlib库绘制饼图的方法示例
    • Python的Bottle框架中获取制定cookie的教程
    • python基础教程之实现石头剪刀布游戏示例
    • 在Python的Tornado框架中实现简单的在线代理的教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有