Cherrypy 和 Jinja2 入门 [英] Getting started with Cherrypy and Jinja2

查看:18
本文介绍了Cherrypy 和 Jinja2 入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次深入研究 Python 中的 Web 开发.我唯一的其他经验是 PHP,我以前从未使用过框架,所以我觉得这非常令人生畏和困惑.

This is my first time delving into web development in python. My only other experience is PHP, and I never used a framework before, so I'm finding this very intimidating and confusing.

我有兴趣学习 CherryPy/Jinja2 为我的 NAS 制作 ZFS 监视器.我已经阅读了 CherryPy/Jinja2 上文档的基础知识,但我发现这些示例不连贯且过于简单,我真的不明白如何让这两件事优雅地融合在一起".

I'm interested in learning CherryPy/Jinja2 to make a ZFS Monitor for my NAS. I've read through the basics of the docs on CherryPy/Jinja2 but I find that the samples are disjointed and too simplistic, I don't really understand how to make these 2 things "come together" gracefully.

我的一些问题:

  1. 是否有一个简单的教程展示了如何让 CherryPy 和 Jinja2 很好地协同工作?我要么找到太简单的样本,比如 CherryPy/Jinja2 文档上的样本,要么找到复杂的方式.(例如:https://github.com/jovanbrakus/cherrypy-example).

是否有一种标准化或预期"的方式来为 CherryPy 创建 Web 应用程序?(例如:我的目录结构应该是什么样子的?有没有办法声明静态的东西;甚至有必要吗?)

Is there a standardized or "expected" way to create web applications for CherryPy? (example: What should my directory structure look like? Is there a way to declare static things; is it even necessary?)

有没有人推荐过这方面的文献或者在线文档是最好的资源?

Does anyone have recommended literature for this or is the online documentation the best resource?

推荐答案

恭喜你选择了 Python,我相信你会像我一样爱上它.

Congratulations on choosing Python, I'm sure you'll learn to love it as have I.

关于 CherryPy,我不是专家,但几天前也和你在同一条船上,我同意这些教程有些部分脱节.

Regarding CherryPy, I'm not an expert, but was also in the same boat as you a few days ago and I'd agree that the tutorials are a little disjointed in parts.

为了集成 Jinja2,如他们的 doc 页面,HTML 的片段应该指定它是模板文件,因此保存在路径/templates/index.html 中.他们还使用了模板代码示例和控制器示例中不匹配的变量.

For integrating Jinja2, as in their doc page, the snippet of HTML should have been specified that it is the template file and as such saved in the path /templates/index.html. They also used variables that didn't match up in the template code sample and controller sample.

下面是一个使用 CherryPy 和 Jinja2 的简单 hello world 的完整工作示例

The below is instead a complete working sample of a simple hello world using CherryPy and Jinja2

/main.py:

import cherrypy
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('templates'))

class Root:
    @cherrypy.expose
    def index(self):
        tmpl = env.get_template('index.html')
        return tmpl.render(salutation='Hello', target='World')

cherrypy.config.update({'server.socket_host': '127.0.0.1',
                         'server.socket_port': 8080,
                        })

cherrypy.quickstart(Root())

/templates/index.html:

<h1>{{ salutation }} {{ target }}</h1>

然后在您的 shell/命令提示符下,使用以下命令为应用程序提供服务:

Then in your shell/command prompt, serve the app using:

python main.py

在您的浏览器中,您应该能够在 http://localhost:8080

And in your browser you should be able to see it at http://localhost:8080

这有望帮助您将 Jinja2 模板连接到您的 CherryPy 应用程序.CherryPy 确实是一个轻量级且非常灵活的框架,您可以在其中选择许多不同的方式来构建您的代码和文件结构.

That hopefully helps you to connect Jinja2 templating to your CherryPy app. CherryPy really is a lightweight and very flexible framework, where you can choose many different ways to structure your code and file structures.

这篇关于Cherrypy 和 Jinja2 入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆