为什么根路径没有路由到我的视图?
Why isn't the root path routing to my view?
如何将根路径(即 /
)路由到视图?这是我的简单设置:
import sys
import wsgiref.simple_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def main(argv):
# Create Application.
with Configurator() as config:
app = config.make_wsgi_app()
# Serve HTTP requests.
server = wsgiref.simple_server.make_server('localhost', 8080, app)
server.serve_forever()
return 0
@view_config(name='')
def page(request):
return Response("Root")
if __name__ == '__main__':
sys.exit(main(sys.argv))
当我请求 http://localhost:8080/
时,我只收到 404 响应。来自日志:
127.0.0.1 - - [14/Apr/2018 09:14:50] "GET / HTTP/1.1" 404 153
响应正文:
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
The resource could not be found.<br/><br/>
</body>
</html>
我是 运行 Python 3.5 和 Pyramid 1.9.1。
您似乎忘记调用 config.scan()
,它会添加您用 @view_config
:
注释的路由
import sys
import wsgiref.simple_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def main(argv):
# Create Application.
with Configurator() as config:
config.scan() # adds routes configured with the decorator
app = config.make_wsgi_app()
# Serve HTTP requests.
server = wsgiref.simple_server.make_server('localhost', 8080, app)
server.serve_forever()
return 0
@view_config(name='')
def page(request):
return Response("Root")
if __name__ == '__main__':
sys.exit(main(sys.argv))
根据 docs:
The mere existence of a @view_config decorator doesn't suffice to perform view configuration. All that the decorator does is "annotate" the function with your configuration declarations, it doesn't process them. To make Pyramid process your pyramid.view.view_config declarations, you must use the scan method of a pyramid.config.Configurator
如何将根路径(即 /
)路由到视图?这是我的简单设置:
import sys
import wsgiref.simple_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def main(argv):
# Create Application.
with Configurator() as config:
app = config.make_wsgi_app()
# Serve HTTP requests.
server = wsgiref.simple_server.make_server('localhost', 8080, app)
server.serve_forever()
return 0
@view_config(name='')
def page(request):
return Response("Root")
if __name__ == '__main__':
sys.exit(main(sys.argv))
当我请求 http://localhost:8080/
时,我只收到 404 响应。来自日志:
127.0.0.1 - - [14/Apr/2018 09:14:50] "GET / HTTP/1.1" 404 153
响应正文:
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
The resource could not be found.<br/><br/>
</body>
</html>
我是 运行 Python 3.5 和 Pyramid 1.9.1。
您似乎忘记调用 config.scan()
,它会添加您用 @view_config
:
import sys
import wsgiref.simple_server
from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.view import view_config
def main(argv):
# Create Application.
with Configurator() as config:
config.scan() # adds routes configured with the decorator
app = config.make_wsgi_app()
# Serve HTTP requests.
server = wsgiref.simple_server.make_server('localhost', 8080, app)
server.serve_forever()
return 0
@view_config(name='')
def page(request):
return Response("Root")
if __name__ == '__main__':
sys.exit(main(sys.argv))
根据 docs:
The mere existence of a @view_config decorator doesn't suffice to perform view configuration. All that the decorator does is "annotate" the function with your configuration declarations, it doesn't process them. To make Pyramid process your pyramid.view.view_config declarations, you must use the scan method of a pyramid.config.Configurator