Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Web.py创作效率_Python_Web.py - Fatal编程技术网

Python Web.py创作效率

Python Web.py创作效率,python,web.py,Python,Web.py,我不熟悉python web框架。我使用web.py是因为我喜欢它的原始状态。但是,我想知道,当被限制通过return关键字发送输出时,如何高效地生成页面和脚本?似乎对于任何python模块,您只能向浏览器发送一个内容,即使它是一个大字符串。关于服务器端脚本的python方式,我遗漏了什么?如果有帮助的话,我是从PHP的角度来看的。我习惯于说打印“foo”,foo就会出现。现在每个模块只允许打印一次。如果您能告诉我python脚本编写方法与php脚本编写方法的区别,我将不胜感激!在这一点上,我非

我不熟悉python web框架。我使用web.py是因为我喜欢它的原始状态。但是,我想知道,当被限制通过
return
关键字发送输出时,如何高效地生成页面和脚本?似乎对于任何python模块,您只能向浏览器发送一个内容,即使它是一个大字符串。关于服务器端脚本的python方式,我遗漏了什么?如果有帮助的话,我是从PHP的角度来看的。我习惯于说打印“foo”,foo就会出现。现在每个模块只允许打印一次。如果您能告诉我python脚本编写方法与php脚本编写方法的区别,我将不胜感激!在这一点上,我非常困惑python如何在如此有限的情况下如此高效

例如,下面是我编写的一个基本程序:

 import web
 urls = ('/','index')
 class index:
     def GET(self):
         return "foo" ///Where this is the only place on the file I can output anything
 (and the rest of the file you should be familiar with)
问题是,在我看来,唯一可以输出任何内容的地方是在一个返回行中?现在我了解到,如果URI参数发生变化,您可以映射到不同的类,从而输出不同的内容,但即使如此,这似乎还是有限的

或者,web框架的最终目的是使用web.py中所称的“模板”吗?
谢谢您的帮助。

是的,这就是模板的作用:有效地将逻辑与表示分离

您必须查询控制器类中的所有数据,然后将其传递给将输出html的模板

这看起来可能不同于普通的php实践,但我相信即使是php开发人员也会选择这种方法


关于web.py,流式内容输出的另一个选项是使用
yield
,请参见Andrey Kuzmin的答案是正确的,模板是正确的工作方式

但是,如果您确实希望生成在php“print”(“blah”);”中返回的文本在某种程度上,你可以这样工作:

class index:
    def GET(self):
        output = []
        # do stuff...
        output.append('Some text.')
        # do more stuff...
        output.append('And more text...')
        # ok, now we're at the end:
        return '<br/>'.join(output)
现在,您将获得一个名为my_application.log的文件,其中包含所有错误和调试消息,如:

appname:DEBUG:This is a debug message
appname:ERROR:Oh no! This should not happen!
要停止显示调试消息,只需将级别从logging.DEBUG更改为logging.ERROR,或者更改为您想要的其他级别,您将只得到错误。链接教程提供了更多信息

我希望这对你有帮助

<?php
    ob_start(); // start caching all output, so it doesn't interfere.
    include('foobar.php');
    do_other_stuff();
    ob_end_flush(); // print the cached output.
?>
import logging
logging.basicConfig(filename='my_application.log', level=logging.DEBUG)
log = logging.getLogger(__name__)

# do stuff....
log.debug('This is a debug message')
# do more stuff
log.error('Oh no! This should not happen!')
# ...
appname:DEBUG:This is a debug message
appname:ERROR:Oh no! This should not happen!