To create a custom 404 error page in Metro, you’ll need to override the default error handling mechanism. It is a web application framework that uses the Python programming language, and it’s built on top of the Werkzeug WSGI toolkit. You can create a custom error page for a 404 error (page not found) by following these steps:
1) Create a custom controller in your module:
In your custom module, create a new Python file (e.g., https://controllers.py%29/ if you don’t already have one, and define a controller class for handling 404 errors. Here’s an example of how you can create a custom controller:
from metro import http
from metro.http import request
class CustomErrorController(http.Controller):
@http.route(‘/page-not-found’, type=’http’, auth=”public”)
def custom_404_error(self, **kwargs):
# You can render a custom 404 HTML template here or return a custom response.
return request.render(“your_module_name.custom_404_template”)
In the code above, we’ve created a controller named CustomErrorController and defined a route /page-not-found to handle 404 errors. You can customize the route and method as needed.
2) Create a custom 404 HTML template:
Now, you need to create a custom HTML template for your 404 error page. In your module’s directory, create a folder called views if it doesn’t already exist. Inside the views folder, create an XML file (e.g., custom_404_template.xml) to define your custom template:
<metro>
<template id=”custom_404_template” name=”Custom 404 Error Page”>
<!– Your custom HTML content for the 404 error page goes here –>
<t t-raw=”0″/>
</template>
</metro>
Replace <!– Your custom HTML content for the 404 error page goes here –> with your HTML code for the error page.
3) Update the module manifest:
In your module’s manifest file (https://__manifest__.py%29%2C/ make sure you declare the controller and XML file:
{
‘name’: ‘Your Module Name’,
‘summary’: ‘Custom 404 Error Page’,
‘description’: ‘Create a custom 404 error page in metro.’,
‘author’: ‘Your Name’,
‘version’: ‘1.0’,
‘depends’: [‘base’],
‘data’: [
‘views/custom_404_template.xml’,
],
‘demo’: [],
‘application’: True,
}
4) Update your web route configuration:
In Metro, you need to add a configuration in your module to point the default 404 error route to your custom controller. To do this, create a https://__init__.py/ file in your module directory and add the following lines:
from . import controllers
5) Restart:
After making these changes, restart your server to apply the modifications.
Now, when a user accesses a non-existent page on your website, they should be redirected to your custom 404 error page defined in the custom_404_template.xml file. Make sure to replace ‘your_module_name’ with the actual name of your module in the code examples above.
To get more details on how to use the Website Builder, please do contact us at support@metrogroup.solutions