在web.py中处理网址

时间:2016-12-03 18:32:04

标签: python web.py

我第一次搞乱web.py,跟随Zed Shaw的学习Python艰难的方式#50。 我试图设置多个网页,但似乎只能获得索引/工作。 我测试了其他一切,一切正常。当我更换

    urls = (
        '/', 'Index',
    )

    urls = (
        '/', 'foo',
    )

加载我的foo网页

但是当我尝试

    urls = (
    '/', 'Index',
    '/foo', 'FOO',

)

并在我的浏览器中输入localhost / foo:8080我收到错误连接被拒绝 我已经杀死了服务器,在我的代码更改之间重新启动它只是为了确保并且没有任何更改。

我尝试了多个例子并且使用了食谱示例无济于事,这让我很难过。 请告诉我我错过了什么。

下面的代码

app.py

    import web

    urls = (
        '/', 'Index',
        '/foo', 'FOO',

    )

    app = web.application(urls, globals())

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

    class Index(object):
        def GET(self):
            greeting = "Hello World"
            return render.index(greeting = greeting)

    class FOO(object):
        def GET(self):
            foo_greeting = "Hello foo"
            return render.foo(foos_greeting = foo_greeting)

    if __name__ == "__main__":
        app.run()

的index.html

    $def with (greeting)

    <html>
        <head>
            <title>Gothons Of Planet Percal #25</title>
        </head>
    <body>

    $if greeting:
        I just wanted to say <em style="color: green; font-size:              2em;">$greeting</em>.
    $else:
        <em>Hello</em>, world!

    </body>
    </html>

foo.html

    $def with (foos_greeting)

    <html>
        <head>
            <title>Gothons Of Planet FOO</title>
        </head>
    <body>

    $if foos_greeting:
        I just wanted to say <em style="color: green; font-size:          2em;">$foos_greeting</em>.
    $else:
        <em>Hello</em>, foo foo!

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

在'FOO'表达式

之后删除逗号
urls = (
'/', 'Index',
'/foo', 'FOO'
)

并运行

localhost:8080/foo