Table of contents
1.
Introduction
2.
URL Rewrite Systems in Web2py
2.1.
Parameter-Based System
2.2.
Pattern-Based System
3.
Routes on Error
4.
Static Asset Management
5.
Frequently Asked Questions
5.1.
What is a pylon framework?
5.2.
Which is better, web2py or Django?
5.3.
What are Django and Flask?
5.4.
What is the web2py framework?
5.5.
What is Falcon API?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

URL Rewrite In Web2py

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Web2py is a Python-based, accessible, and open-source web framework enabling agile development of database-driven online applications. It is a full-stack framework that includes all the elements a developer needs to create a fully functional web application.

Web2py cover

Web2py can rewrite the URL path of incoming requests before running the controller action (URL mapping), and it can also rewrite the URL path generated by the URL function (reverse URL mapping).

Let’s learn how to rewrite URLs in web2py.

URL Rewrite Systems in Web2py

Web2py contains two different URL rewrite systems: a simple parameter-based system for most use cases and a flexible pattern-based system for more complicated applications. Create a new file named routes.py in the “web2py” folder to provide the URL rewriting rules (the contents of routes.py will depend on which of the two rewrite systems you choose). Both systems must be used separately.

Note: If you make changes to routes.py, you must reload it. There are two methods to accomplish this: either restart the web server or click the Routes Reload option in the admin. Routes will not reload if they contain a bug.

Let’s learn about these URL rewrite systems.

Parameter-Based System

The parameter-based (parametric) router allows quick access to numerous "canned" URL-rewrite techniques. Its capabilities include the following:-

capabilities of parameter based system

Pattern-Based System

Although the parameter-based method described above should provide for most use cases, the alternative pattern-based system gives some extra flexibility for more complicated scenarios. To utilize the pattern-based system, instead of describing routers as dictionaries of routing parameters, you declare two lists (or tuples) of 2-tuplesroutes_in, and routes_out. Each tuple has two elements: the pattern to be replaced and the string that replaces it. For example:-

routes_in = (
    ('/ninjas', '/coding/help'),
)
routes_out = (
    ('/coding/help', '/ninjas'),
)
You can also try this code with Online Python Compiler
Run Code

Routes on Error

You can also use routes.py to re-route requests to particular actions if the server encounters an issue. You can set this mapping globally, for each app, for each error code, or for each app and error code. Let’s understand this with an example:-

routes_onerror = [
    ('init/400', '/init/default/login'),
    ('init/*', '/init/static/fail.html'),
    ('*/404', '/init/static/cantfind.html'),
    ('*/*', '/init/error/index')
]
You can also try this code with Online Python Compiler
Run Code

 

The first string of each tuple is compared to "[app name]/[error code]". If a match is found, the unsuccessful request is sent to the URL in the matched tuple's second string. If the error handling URL is not a static file, the following GET variables will be provided to the error action:-

GET variables for the error

Static Asset Management

Static files might often change during the development of an application, hence web2py delivers static files without any cache headers. This has the unintended consequence of "forcing" the browser to request static files with each request. As a result, the page loads slowly.

Because static files do not change, you may wish to serve static files with cache headers on a "production" site to avoid unnecessary downloads.

cache headers allow the browser to fetch each file just once, saving bandwidth and time.

Static asset management overcomes the problem by allowing the developer to designate a version for a group of static files, and they will only be requested again when the version number changes. 

Frequently Asked Questions

What is a pylon framework?

Python-based Pylons Framework is an open-source Web application framework. The Web Server Gateway Interface standard is heavily utilized to encourage reusability and divide functionality into independent modules.

Which is better, web2py or Django?

Due to its smaller size, simpler learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a significantly more straightforward syntax.

What are Django and Flask?

While Django is a high-level web framework for Python, Flask is a micro-framework. As a result, the flask is considerably simpler to comprehend and learn. Both are open-source platforms. However, the flask is more commonly used for lightweight apps than Django. Both are utilized differently and for various purposes.

What is the web2py framework?

Web2py, which is written in Python and programmable in Python, is described as a free, open-source online framework for agile development that involves database-driven web applications.

What is Falcon API?

Falcon is a lightning-quick, lightweight Python web API framework for creating reliable app backends and microservices. The framework performs admirably with both gevent/meinheld and asyncio (ASGI) (WSGI).

Conclusion

In this article, we have extensively discussed URL rewrite in Web2py and different URL rewrite systems. If you want to learn more, check out our articles on What Is Web2Py?Why To Use Web2py?Postbacks and Internationalization in web2py, and  XML in Web2py.

Do upvote our blog to help other ninjas grow.

Happy Coding!

Thankyou image
Live masterclass