• 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的例子

相关文章

  • Django查找网站项目根目录和对正则表达式的支持
  • python文件与目录操作实例详解
  • python中函数默认值使用注意点详解
  • python编程实现归并排序
  • Pythont特殊语法filter,map,reduce,apply使用方法
  • Python 中 list 的各项操作技巧
  • python连接字符串的方法小结
  • python保存字符串到文件的方法
  • Django中对通过测试的用户进行限制访问的方法
  • python动态文本进度条的实例代码

文章分类

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

最近更新的内容

    • python3中str(字符串)的使用教程
    • Python的消息队列包SnakeMQ使用初探
    • 使用Python来编写HTTP服务器的超级指南
    • 在Django的模型中执行原始SQL查询的方法
    • 在SAE上部署Python的Django框架的一些问题汇总
    • Python字符串转换成浮点数函数分享
    • Python实现获取域名所用服务器的真实IP
    • Python读写Json涉及到中文的处理方法
    • 详解Python3中字符串中的数字提取方法
    • 跟老齐学Python之大话题小函数(2)

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

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