Odoo 中的 Controller


Odoo處理HTTP請求的接口用的Contoller類,封裝於web模塊中。

---------------------------------------------------------------

RequestHandler:

1. replace_request_password(args):用*替換掉request中的密碼字符。

2. dispatch_rpc(service_name, method, params):處理RPC請求。service_name的值可取common,db,object,report四種。

3. local_redirect(path, query=None, keep_hash=False, forward_debug=True, code=303):重定向到一個新url。

4. redirect_with_hash(url, code=303):帶locathion.hash值的重定向方法。

---------------------------------------------------------------

WebRequest:

Odoo Web請求的父對象

屬性:httprequest,httpresponse,httpsession,env,context,session,lang,cr,debug,registry_cr,session_id,registry,db

---------------------------------------------------------------

route裝飾器:封裝了處理web request路由的處理方法,被封裝的方法必須為Controller的子類方法。

route(route=None, **kw):

參數說明:

route:字符或數組,映射URL中對應的路徑。

type:request的類型,'http'或'json'。

auth: 認證方法,可以為以下值:'user','admin','none'.

methods: http請求方法,默認為都允許。(GET,POST)

cors:跨域指示值。

--------------------------------------------------------------

JsonRequest:WebRequest子類,處理JSON-RPC(http://wiki.geekdream.com/Specification/json-rpc_2.0.html)的類。

--------------------------------------------------------------

HttpRequest:WebRequest子類,處理HTTP請求並響應。

1.make_response:處理非HTML響應或自定義的headers和cookies

2.render:顯示QWeb模板

3.not_found:404

 

例子1:自定義路徑/px 返回字符串“Hello Odoo".    

 

import openerp
from openerp import http

class px(openerp.addons.web.controllers.main.Home):

    @http.route(['/px'],type='http',auth='None')
    def px2(self,*args,**kargs):
        return  'Hello Odoo!'

 

例子2:自定義路徑/px 重定向到網址 bing.com

 

import openerp
from openerp import http
from openerp.http import local_redirect_with_hash

class px(openerp.addons.web.controllers.main.Home):

    @http.route(['/px'],type='http',auth='public')
    def px2(self,*args,**kargs):
        return  local_redirect_with_hash('http://www.bing.com')

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM