推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
paloalto
V2EX  ›  Python

请教web.py中自定义404错误页的问题。

  •  
  •   paloalto · Dec 5, 2012 · 3354 views
    This topic created in 4925 days ago, the information mentioned may be changed or developed.
    代码如下:



    访问 http://0.0.0.0:8080/post/123test 时,因为url不匹配,会跳转到我自定义的404页面:view.error();




    但是访问 http://0.0.0.0:8080/post/123 这种url,如果ID为123的post不存在,就直接在页面中显示 “404 Not Found”了,没法跳转到我自定义的404错误页面。




    请问怎么才能让这两种情况都跳转到我自定义的404错误页面呢?
    5 replies    1970-01-01 08:00:00 +08:00
    Js
        1
    Js  
       Dec 5, 2012
    tokki
        2
    tokki  
       Dec 5, 2012
    ls好黑
    paloalto
        3
    paloalto  
    OP
       Dec 5, 2012
    @Js 谢谢你的回复。我是google了“web.py 自定义 404”之后,按照google搜索结果中的web.py的教程写的. raise 404我已经做到了,但是出现了两种404的情况,请问是何原因呢?
    paloalto
        4
    paloalto  
    OP
       Dec 5, 2012
    @paloalto 哈哈,我知道原因了。
    paloalto
        5
    paloalto  
    OP
       Dec 5, 2012
    需要在app.controllers.post.post_show中写上 raise web.notfound() :

    def GET(self,node_id):
    ----if node:
    --------return view.node_show()
    ----else:
    --------raise web.notfound()
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5500 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 08:13 · PVG 16:13 · LAX 01:13 · JFK 04:13
    ♥ Do have faith in what you're doing.