Introduction to odoo controller
In Odoo, a controller is a key component of the web framework that handles the interaction between the user interface (UI) and the backend logic. Controllers are responsible for defining how requests to the server are processed and how responses are generated. They act as an intermediary, managing the flow of data between the client and the server.
Routing: Controllers define routes, which are URL patterns that map to specific methods within the controller. These routes determine how requests from the client are handled.
Request Handling: Controllers process incoming HTTP requests. They can receive data from the client, perform operations or computations, and then return appropriate responses.
Rendering Views: Controllers can render views (HTML pages) to be displayed to the user. They can pass data to these views, enabling dynamic content generation based on user actions or other inputs.
Interacting with Models: Controllers can interact with Odoo models to read, create, update, or delete records in the database. This is essential for performing CRUD (Create, Read, Update, Delete) operations.
Security and Authentication: Controllers often include security checks to ensure that only authorized users can access certain routes or perform specific actions. They handle user authentication and permissions.
from odoo import http
from odoo.http import request
class MyController(http.Controller):
@http.route('/my_controller/hello', auth='public')
def hello_world(self, **kw):
return "Hello, World!"
@http.route('/my_controller/greet', auth='public', methods=['GET'], type='http', csrf=False)
def greet(self, **kw):
name = kw.get('name', 'Guest')
return "Hello, {}!".format(name)
@http.route('/my_controller/data', auth='user', methods=['POST'], type='json')
def data(self, **kw):
data = request.jsonrequest
# Process data and interact with models if needed
return {"status": "success", "data_received": data}
class OnboardingController(http.Controller):
@http.route('/', type='http', auth='public', website=True)
def render_report_page(self):
onboardings=request.env['onboarding.appointing'].sudo().search([])
return http.request.render('on_boarding.home_page', {'onboardings':onboardings})
class PartnerController(http.Controller):
@http.route('/partner', type='http', auth='public', website=True)
def render_report_page(self):
partners=request.env['res.partner'].sudo().search([])
return http.request.render('on_boarding.partner_view', {'partners':partners})
@route('/create/patient',type='http' auth='public',website=True)
def create_patient(self,**kw):
request.env['hospital.patient'].sudo().create(kw)
retrun request.render('hospital.patients_thanks',{})
Defining the Controller Class:
MyController class inherits from http.Controller.Route Decorators:
@http.route decorators define the URL patterns that the controller methods will respond to.auth='public' means that the route is accessible without authentication.methods specify the HTTP methods that the route accepts (e.g., GET, POST).type='http' indicates a standard HTTP response, while type='json' indicates a JSON response.csrf=False can be used to disable CSRF protection for specific routes, though it should be used with caution.Controller Methods:
hello_world: A simple method that returns a plain text response.greet: A method that reads a name parameter from the query string and returns a personalized greeting.data: A method that accepts a JSON payload, processes the data, and returns a JSON response.
Then you need to define template in inside views directory and mantion path in manifest path
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data>
<record id="menu_onboarding_partner" model="website.menu">
<field name="name">Partner</field>
<field name="url">/partner</field>
<field name="parent_id" ref="website.main_menu"/>
<field name="sequence">56</field>
</record>
<template id="home_page" name="OnBoarding">
<title>odoo-onboaring</title>
<t t-call="website.layout">
<!-- website.layout will add header and footer in website -->
<div class="container">
<div class="row">
<h4 class="mt-5 text-center">Onboarding List</h4>
<div class="col-md-12">
<table class="table table-success table-striped table-hover my-5">
<thead class="table-dark">
<tr>
<th>S.N.</th>
<th>Name</th>
<th>Phone</th>
<th>Email</th>
<th>Gender</th>
<th>Department</th>
<th>Created By</th>
<th>Created On</th>
</tr>
</thead>
<tbody>
<t t-set="counter" t-value="1"/>
<t t-foreach="onboardings" t-as="onboarding">
<tr>
<td><t t-out="counter"/></td>
<td><t t-out="onboarding.name"/></td>
<td><t t-out="onboarding.phone"/></td>
<td><t t-out="onboarding.email"/></td>
<td><t t-out="onboarding.gender"/></td>
<td><t t-out="onboarding.department_id.name"/></td>
<td><t t-out="onboarding.create_uid.name"/></td>
<td><t t-out="onboarding.create_date.strftime('%Y-%m-%d %H:%M')"/></td>
</tr>
<t t-set="counter" t-value="counter + 1"/>
</t>
</tbody>
</table>
</div>
<div class="col-md-12 my-5">
<img src="/on_boarding/static/description/2.png" class="img-fluid" alt="Onboarding Image" style="border: 2px solid red; border-radius: 10px; height:70%;width:100%;object-fit:cover;"/>
</div>
</div>
</div>
</t>
</template>
<template id="partner_view" name="Partners">
<title>odoo-partners</title>
<t t-call="website.layout">
<div class="container">
<h4 class='mt-5 text-center'>Partner List</h4>
<table class="table table-success table-striped table-hover my-5">
<thead class="table-dark">
<tr>
<th>S.N.</th>
<th>Name</th>
<th>Email</th>
<th>Created On</th>
</tr>
</thead>
<tbody>
<t t-set="counter" t-value="1"/>
<t t-foreach="partners" t-as="partner">
<tr>
<td><t t-out="counter"/></td>
<td>
<t t-out="partner.name"/>
</td>
<td>
<t t-out="partner.email"/>
</td>
<td>
<t t-out="partner.create_date.strftime('%Y-%m-%d %H:%M')"/>
</td>
</tr>
<t t-set="counter" t-value="counter + 1"/>
</t>
</tbody>
</table>
</div>
</t>
</template>
</data>
</odoo>
Amanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyBaltej Singh 5 days ago
Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
ReplyMarie Johnson 5 days ago
Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
Reply