How it works…

In the first step, we overrode an existing QWeb template. In order to find out which one it is, you'll have to consult the code of the original handler. Usually, this will end up with something similar to the following line, which tells you that you need to override template.name:

return request.render('template.name', values) 

In our case, the handler used a template called website_info, but this one was immediately extended by another template called website.show_website_info, so it's more convenient to override this one. Here, we replaced the definition list showing installed apps with a table. For details on how QWeb inheritance works, consult Chapter 16, Web Client Development.

In order to override the handler method, we must identify the class that defines the handler, which is odoo.addons.website.controllers.main.Website, in this case. We need to import the class to be able to inherit from it. Now, we can override the method and change the data passed to the response. Note that what the overridden handler returns here is a Response object and not a string of HTML like the previous recipes did, for the sake of brevity. This object contains a reference to the template to be used and the values accessible to the template, but it is only evaluated at the very end of the request.

In general, there are three ways to change an existing handler:

  • If it uses a QWeb template, the simplest way to change it is to override the template. This is the right choice for layout changes and small logic changes.
  • QWeb templates get a context passed, which is available in the response as the qcontext member. This is usually a dictionary where you can add or remove values to suit your needs. In the preceding example, we filtered the list of apps to the website only.
  • If the handler receives parameters, you can also preprocess those, in order to have the overridden handler behave in the way you want.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset