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

查看:90
本文介绍了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 docs上的样本,或者发现了复杂的方式. (示例: 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,如其文档页面(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的简单问候世界的完整工作示例.

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天全站免登陆