1. Introduction
Adding and optimizing SEO is one of the most important parts for a web based application. If you have an online store, portal or a lot of online content you should take the time to create good SEO to improve your ranking. In most cases your records have a rather identical structure and you will want to set default values as meta data. In this tutorial you will learn three things:
- Adding SEO possibilities to your own model(s)
- Settings default metadata
- Customize social media previews: preview image, title and text
In this tutorial we will create a new model named 'tutorial' and website pages for all tutorial records. We'll dynamically set default metadata for all tutorials and modify the social media previews so they better match with our tutorials.
2. Inheriting the abstract model for SEO
By default Odoo has the "Optimize SEO" option in every webpage of Odoo under the "Customize" menu at the top of the page. There are a few limitations though:
- Your SEO values are not shown in the backend of Odoo.
- There is no description and there are no keywords predefined.
- The preview image for social media is often not the right one (most likely the company logo).
In order to resolve these three limitations we first have to inherit the model 'website.seo.metadata'. This model is an abstract model, which means that we can inherit it to add the fields and functions that are available for this model on our model. Let us create a new model named 'tutorial' that inherits the seo model. We'll add a name, description, author and preview_image field right away too:
# -*- coding: utf-8 -*-from odoo import models, fields, api, modules, _class Tutorial(models.Model): _name = 'tutorial' _inherit = ['website.seo.metadata'] _description = 'Tutorial for a specific object' name = fields.Char(string='Tutorial name', required=True, copy=False) preview_image = fields.Binary("Image preview", copy=False) author_id = fields.Many2one('res.partner', string='Author', copy=False) description = fields.Text(string='Intro (preview) text', copy=False)
3. Creating the backend
3.1 Creating the views
Let's first create a menuitem, tree view and form view so that we can create our records. Since we have inherited the model 'website.seo.metadata' we inherit the fields of this model. We can now show the fields 'website_meta_title', 'website_meta_description' and 'website_meta_keywords' on the form so that you can fill in the SEO from the backend too:
3.2 Creating the menus and action
First create an action to view all tutorials and a main menuitem. The action should reference our search view and the menuitem should reference the action:
Finally add a child menuitem for easy navigation:
This is all the code that we need to view and modify our records in the backend. We now need security rules to be able to view the content.
4. Creating security rules
The next thing we need is security rules for this model or we won't be able to view/modify any record. Open up the 'ir.model.access.csv' file and add two security rules. One for logged in users so they can do all operations and one for public (website) users so they can view the content (which we will show on the website later on):
access_tutorial,tutorial,model_tutorial,base.group_portal,1,1,1,1public_access_tutorial,tutorial,model_tutorial,base.group_public,1,0,0,0
Tip: don't forget to import the ir.model.access.csv file in your __manifest__.py!
5. Creating a controller
5.1 Tutorials overview
Now that we have the backend ready we should move to the frontend. Let's create two controller functions. The first one will show all tutorials if we open the website and go to /tutorials. The second one will open a tutorial if the user clicked on a tutorial in the overview. Create a new file named 'tutorial.py' in the 'controllers' folder and create a route that gets all tutorials and makes them available:
# -*- coding: utf-8 -*-from odoo import httpfrom odoo.http import requestclass Tutorial(http.Controller): @http.route(["/tutorials"], type='http', auth="public", website=True) def tutorial_overview(self, **kw): # Get all tutorials from the database tutorials = http.request.env['tutorial'].search([]) # Return the tutorial overview page with all tutorial records return request.render('tutorial_optimize_seo.tutorial_overview', {'tutorials': tutorials})
This controller will load all tutorials from the database if you surf to yourwebsite.com/tutorials and will load the XML page named "tutorial_overview" to show it to the user. Our next step is to create an XML record to show all tutorials on this page. Create a new file named 'tutorial_frontend_view.xml' in the 'views' folder of your module and create a new template. Let us show the name of the tutorial, the description and the author. The name will be shown as a title and should create a link to a detail page: