- Python Pyramid - Discussion
- Python Pyramid - Useful Resources
- Python Pyramid - Quick Guide
- Python Pyramid - Deployment
- Python Pyramid - Security
- Python Pyramid - Logging
- Python Pyramid - Testing
- Command Line Pyramid
- Creating A Project Manually
- Python Pyramid - Package Structure
- Python Pyramid - Project Structure
- Python Pyramid - Creating A Project
- Python Pyramid - Cookiecutter
- Pyramid - Using SQLAlchemy
- Python Pyramid - Message Flashing
- Python Pyramid - Events
- Python Pyramid - Sessions
- Python Pyramid - Response Object
- Python Pyramid - Request Object
- Python Pyramid - Static Assets
- Pyramid - HTML Form Template
- Python Pyramid - Templates
- Python Pyramid - Route Prefix
- Python Pyramid - View Configuration
- Python Pyramid - Url Routing
- Pyramid - Application Configuration
- Python Pyramid - Hello World
- Pyramid - Environment Setup
- Python Pyramid - Overview
- Python Pyramid - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Pyramid - Route Prefix
Many times, similar URL patterns are registered with different routes in more than one Python code modules. For example, we have a student_routes.py where /pst and /add URL patterns are registered with pst and add routes. The view functions associated with these routes are pst() and add(), respectively.
#student_routes.py from pyramid.config import Configurator from pyramid.response import Response from pyramid.view import view_config @view_config( route_name= add ) def add(request): return Response( add student ) @view_config(route_name= pst ) def pst(request): return Response( Student pst ) def students(config): config.add_route( pst , /pst ) config.add_route( add , /add ) config.scan()
These routes will eventually be registered when the students() function is called.
At the same time, there is book_routes.py, in which the same URLs /pst and add/ are registered to show and new routes. Their associated views are pst() and add() respectively. The module has books() function which adds the routes.
#book_routes.py from pyramid.config import Configurator from pyramid.response import Response from pyramid.view import view_config @view_config( route_name= new ) def add(request): return Response( add book ) @view_config(route_name= show ) def pst(request): return Response( Book pst ) def books(config): config.add_route( show , /pst ) config.add_route( new , /add ) config.scan()
Obviously, there is a confpct between URL patterns as /pst and /add point to two routes each and this confpct must be resolved. This is done by using the route_prefix parameter of the config.include() method.
The first parameter to config.include() is the function which adds the routes, and the second is the route_prefix string which will be prepended to the URL pattern used in the included function.
Hence, the statement
config.include(students, route_prefix= /student )
will result in the /pst URL pattern changed to /student/pst and /add becomes student/add . Similarly, we can add prefix to these URL patterns in the books() function.
config.include(books, route_prefix= /books )
Example
The code that starts the server is as below −
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Response from student_routes import students from book_routes import books if __name__ == __main__ : with Configurator() as config: config.include(students, route_prefix= /student ) config.include(books, route_prefix= /book ) app = config.make_wsgi_app() server = make_server( 0.0.0.0 , 6543, app) server.serve_forever()
Output
Let us run the above code and test the routes by following CURL commands.
C:UsersAcer>curl localhost:6543/student/pst Student pst C:UsersAcer>curl localhost:6543/student/add add student C:UsersAcer>curl localhost:6543/book/add add book C:UsersAcer>curl localhost:6543/book/pst Book pstAdvertisements