【问题标题】:Url handling in web.pyweb.py 中的 URL 处理
【发布时间】:2016-12-03 18:32:04
【问题描述】:

在 Zed Shaw 的 Learn Python The Hard Way tut# 50 之后,我第一次搞砸了 web.py。 我正在尝试设置多个网页,但似乎只能让 index / 工作。 我已经测试了其他所有内容,并且一切正常。当我更换

    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>

【问题讨论】:

    标签: python web.py


    【解决方案1】:

    删除'FOO'表达式后的逗号

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

    然后运行

    localhost:8080/foo
    

    【讨论】:

    • 逗号不是问题,但是是的,您需要在主机之后添加端口号:8080,而不是在/foo 路径之后。
    猜你喜欢
    • 2013-01-18
    • 1970-01-01
    • 2012-12-19
    • 2012-05-27
    • 2014-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 1970-01-01
    相关资源
    最近更新 更多