Building new website in odoo14
- We can create a simple Website in odoo
Now you need to create a new database including demostration data.Then you have to write the code for creating website,
Step 1:
Add a controller and ensure it is imported by __init__.py
file. (Controllers interpret browser requests and send data back.)
eg: from odoo import http
class Academy(http.Controller):
@http.route(‘/send/mail’, auth=‘public’)
def send_mail(self, **kw):
return “Hello, world”
Now open a page to http://Localhost:8069/send/mail you should see your “page” appear:
Hello,World!
Step 2:
Create a template and ensure the template file is registered in the __manifest__.py
manifest, and alter the controller to use our template:
eg:class Academy(http.Controller):
@http.route(‘/send/mail’, auth=‘public’)
def send_mail(self, **kw):
return http.request.render(‘academy.send_mail’, {
‘teachers’: [“Diana Padilla”, “Jody Caroll”, “Lester Vaughn”], })
Step 3:
Create corresponding xml
eg: <odoo>
<template id=“index”>
<title>Academy</title>
<t t-foreach=“teachers” t-as=“teacher”>
<p><t t-esc=“teacher”/></p>
</t>
</template>
</odoo>
Here we use <t t-foreach> for iteration. Now the result will be Like this:
Step 4:
You can Define the models in python file
eg: from odoo import models, fields, api
class Teachers(models.Model):
_name = ‘academy.teachers’
name = fields.Char()
Step 5:
Add the class name in security: ir.model.access.csv file also add the csv file in ___manifest__.py.
Step 6:
Add Corresponding xml file
eg: <odoo>
<record id=“padilla” model=“academy.teachers”>
<field name=“name”>Diana Padilla</field>
</record>
<record id=“carroll” model=“academy.teachers”>
<field name=“name”>Jody Carroll</field>
</record>
<record id=“vaughn” model=“academy.teachers”>
<field name=“name”>Lester Vaughn</field>
</record>
</odoo>
do not forget to add manifest file.
Step 7:
Accessing Data
In controllers.py we need to add this code
class Academy(http.Controller): @http.route('/send/mail', auth='public') def send_mail(self, **kw): Teachers = http.request.env['academy.teachers'] return http.request.render('academy.send_mail', { 'teachers': Teachers.search([]) }) Here we need to fetch the records from the database instead of having a static list
Because search()
returns a set of records matching the filter (“all records” here), alter the template to print each teacher’s name
Add this code into last we created xml file
eg:
<odoo> <template id="index"> <title>Academy</title> <t t-foreach="teachers" t-as="teacher"> <p><t t-esc="teacher.id"/> <t t-esc="teacher.name"/></p> </t> </template> </odoo> Then
- first, add
website
as a dependency to send_mail - then add the
website=True
flag on the controller, this sets up a few new variables on the request object and allows using the website layout in our template - use the website layout in the template
Then you can run your module: