odoo路由器系列(Controllers)

你們好,javascript

今天,咱們來說講odoo 中的 Controller(路由器),它的主要功能,是提供將web頁面中的信息,進行路由。css

首先,咱們但願獲得的最終效果樣式:一個網站頁面,包含:公司、用戶、聯繫人等等信息。前端

example_page_2

步驟1:建立路由器java

在你建立一個網頁前,你須要作的第一步是建立一個路由器(Controller)。這個路由器,會告訴Odoo 某個URL的具體網頁指向。打開您的模塊,建立一個文件夾,將其命名爲「Controllers」。python

在建立好後,新建一個_init_文件,並添加如下代碼:jquery

# -*- coding: utf-8 -*- from . import example 

這樣,你已經成功把example文件,引入odoo庫
步驟1.1:建立路由器函數web

讓咱們來建立第一個路由器,首先,咱們先看看下面的一段代碼:數據庫

# -*- coding: utf-8 -*- from odoo import http class Example(http.Controller): @http.route('/example', type='http', auth='public', website=True) def render_example_page(self): return http.request.render('create_webpage_demo.example_page', {}) @http.route('/example/detail', type='http', auth='public', website=True) def navigate_to_detail_page(self): # This will get all company details (in case of multicompany this are multiple records) companies = http.request.env['res.company'].sudo().search([]) return http.request.render('create_webpage_demo.detail_page', { # pass company details to the webpage in a variable 'companies': companies}) 

這段代碼的含義是什麼吶?後端

@http.route 會告訴odoo 咱們但願連接 /example 至 一個指定頁面。這個方法裏,咱們看到了4個函數:瀏覽器

‘/example’: 這段代碼用於定義,URL的網頁指向

type=’http’:這段代碼,用於說明網頁運行於http協議。(其餘協議:)

auth=’public’:告訴Odoo誰能夠訪問網頁。(其餘選項包含:’user’,’public’,’none’)

website=’True’:說明,此方法爲網頁

步驟1.2:告訴odoo頁面位置

返回方法,告訴odoo哪一個位置來調用原始頁面。

return http.request.render('create_webpage_demo.example_page', {}) 

這段代碼,http.request.render 會調用odoo架構裏的 渲染器。用來渲染出,相應的效果。這裏的create_webpage_demo是咱們的模塊名,example_page是咱們的文件名。
注意:方法裏有個{},這個字典的主要用途是來進行傳值,返回給網頁。後面會進一步講到。

步驟2:建立網頁視圖

在manifest.py文件中,建立視圖文件,example_webpage.xml

而後,編輯文件: 【首先,讓咱們看看代碼】

<odoo>
  <data> <template id="example_page" name="Example page" page="True"> <t t-call="website.layout"> <div class="oe_structure"> <div class="container"> <center><h3>Title</h3></center> <p> You can add all your content here.<br/> <a t-attf-href="/example/detail" class="btn btn-info">Company detail page</a> </p> </div> </div> </t> </template> </odoo> 

代碼一貫比較冗長,很難看懂。咱們一步一步來看。首先,咱們看到有個id = 「example_page」。這是什麼?這裏就是咱們前面用路由器(Controllers)裏面定義的頁面。這樣,odoo就能夠知道,咱們須要它渲染的頁面是當前咱們正在編輯的代碼。

而後,咱們看到 page="True" 標籤,這樣odoo架構才能夠知道須要將此xml渲染爲網頁,而不是單純的數據返回。接下來:

 

這段代碼,是由於前端界面是不少css 文件,jquery 文件等等組成,咱們的網頁須要經過調用odoo原生的視圖模板,並把當前xml文件中的值,填入原生視圖模板中後,進行渲染出相應的網頁。

這裏的,

,就是用來帶出相應的css、js、jquery等等文件。

步驟2.1:查看視圖

如今,咱們就能夠來看看,咱們寫出來的代碼最後是什麼樣子。

打開瀏覽器,進入 example 界面,你能夠看到如圖所示的網頁。
example_web_page

步驟2.2:模塊方法二

咱們如今爲咱們的控制器,建立第二個方法。

讓咱們看看如下代碼:

@http.route('/example/detail', type='http', auth='public', website=True) def navigate_to_detail_page(self): # This will get all company details (in case of multicompany this are multiple records) companies = http.request.env['res.company'].sudo().search([]) return http.request.render('create_webpage_demo.detail_page', { # pass company details to the webpage in a variable 'companies': companies}) 

這段代碼裏,咱們把數據經過 字典{},傳入網頁前端。

步驟2.3:方法傳值

咱們看到,由於沒有任何代碼用來傳值,您也能夠在路由器中讀取數據庫中的值。

一般狀況下,咱們使用下面的方法在模型中調用數據庫的值。
companies = self.env['res.company'].search([])
可是,在 路由器中,咱們是沒法使用這個方法的。

必須使用,下面的方法進行數據庫數據調用。
companies = http.request.env['res.company'].sudo().search([])
你們看到,咱們這裏添加了 sudo() 方法,爲何?由於,咱們但願路由器中的方法,像管理員同樣,能夠隨意調用數據庫中的數據,同時不會遇到任何安全問題。
注意:這裏,咱們將全部的公司數據都傳入的前端。【{'companies': companies}】
**步驟3:詳細視圖
**
最後,咱們來建立咱們的詳細視圖。

<template id="detail_page" name="Detail page" page="True"> <t t-call="website.layout"> <div class="oe_structure"> <div class="container"> <center><h3>Company detail page</h3></center> <t t-foreach="companies" t-as="company"> <h4><span t-esc="company.name"/></h4> <table class="table-striped table"> <tr> <td>Phone:</td> <td><span t-esc="company.phone"/></td> </tr> <tr> <td>E-mail:</td> <td><span t-esc="company.email"/></td> </tr> <tr> <td>Address:</td> <td> <span t-esc="company.street"/> <span t-esc="company.street2"/><br/> <span t-esc="company.city"/> <span t-esc="company.country_id.name"/> </td> </tr> </table> </t> </div> </div> </t> </template> 

初看這段代碼,一臉懵逼。首先,路由器經過 detail_page 定位到當前這個文件。

而後,咱們用了一段循環語句,t t-foreach 來將從後端返回的數據進行前端展現。

<t t-foreach="companies" t-as="company"> // 這就是循環,相似C 裏面的Loop 

由於,這裏使用了一個foreach語句,您能夠訪問一切公司數據。訪問語法爲:」company.field_name」

<h4><span t-esc="company.name"/></h4> 

步驟4:查看效果
最後,就生成了咱們但願看到的網頁。(訪問: /example/detail)
example_web_page_3

相關文章
相關標籤/搜索