简体   繁体   English

web.py定义/ x在模板中转到x.html?

[英]web.py define /x to go to x.html in templates?

In web.py how can I define going for example to example.com/x to go to a file inside templates folder called x.html ? 在web.py中,如何定义转到example.com/x的 路径,以转到名为x.html的模板文件夹中的文件?

Very new to web.py and would like to know how to do this! 对web.py来说还很陌生,想知道如何做! Their documentation doesn't explain much of anything to me. 他们的文档对我没有太多解释。

According to this page , you can do it like this: 根据此页面 ,您可以这样操作:

urls = (
    '/(.+)', 'your_handler_class'
)

class your_handler_class:
    def GET(self, page):
        print web.template.render('%s.html' % page)

This is basic URL handling in web.py. 这是web.py中的基本URL处理。

For your example, do the following: 对于您的示例,请执行以下操作:

urls = (
    "/x", "x"
)

This assumes you have defined a directory for your templates and created that directory one level deep (or wherever), for example as follows: 假设您已经为模板定义了一个目录,并在一个级别(或任何位置)的深度上创建了该目录,例如:

render = web.template.render('templates/')

web.py will then look in the templates directory for x.html when you render the page, usually with a GET handler, as such: 呈现页面时,通常使用GET处理程序,然后web.py将在x.html的模板目录中查找,例如:

class x:
    def GET(self):
        return render.x()


In this case, both your class and your template are named x, but this doesn't need to be the case. 在这种情况下,您的类和模板都被命名为x,但这不是必须的。

For example, you could have named your template 'xyz.html' and called it with return render.xyz() , and still named your class 'x' and mapped it to /x 例如,您可以将模板命名为'xyz.html'并使用return render.xyz()对其进行调用,而仍将其命名为'x'并将其映射到/ x

Just use getattr passing it render and page name captured by url regexp. 只需使用getattr传递它,即可render url regexp捕获的render和页面名称。

urls = (
  '/(.+)?', 'PageController'
)

render = web.template.render('templates/')

class PageController:

    def GET(self, page='index'):
        try:
            return getattr(render, page)()
        except AttributeError:
            raise web.notfound()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM