How do you route the root path (i.e., /
) to a view? This is my simple setup:
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))
When I request http://localhost:8080/
, I'm just getting a 404 response. From the log:
127.0.0.1 - - [14/Apr/2018 09:14:50] "GET / HTTP/1.1" 404 153
Response body:
<html>
<head>
<title>404 Not Found</title>
</head>
<body>
<h1>404 Not Found</h1>
The resource could not be found.<br/><br/>
</body>
</html>
I'm running Python 3.5 and Pyramid 1.9.1.
Looks like you forgot to invoke config.scan()
, which adds the routes that you annotate with @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))
Per the 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