根據我的理解,實際上並沒有使用t-esc語法從模板中調用方法。你可以做的是在你的控制器中創建方法,這些方法可以通過帖子訪問,或者獲取請求。如果您願意,您可以使用JavaScript使用您的模板發出這些請求,或者通過標記將JavaScript文件包含在您的模板中。如果腳本很簡單,你也許可以逃脫內聯JavaScript
<script>console.log("Hello World")</script>
否則,您可以在js文件你這樣的靜態目錄內點。
<script src="/<module>/static/main.js"></script>
但是,Odoo對於包含js有一些規則。如果您使用Odoo CMS作爲Qweb模板的父模板,那麼您可能還想使用xpath將您的js文件與Odoo的所有js文件的其餘部分一起放置。
<template id="my_js" inherit_id="website.assets_frontend" name="My Js">
<xpath expr="script[last()]" position="after">
<script type="text/javascript" src="/<module>/static/main.js" />
</xpath>
</template>
在Odoo9事情變得(可能),因爲你需要使用要求JS語法來js.Here中使用POST請求更復雜的是一個過於簡單的例子,應在Odoo8工作。對於Odoo9,請查看使用require js的其他示例的代碼。
這裏是一些控制器方法,一個json,另一個http。把你的代碼放在適合你的任何地方。您可以選擇返回更合適的東西(如某些數據),但是True或False表示成功或失敗可能就足夠了。
@http.route('/test/json/method/', auth='none', type='json', website=True)
def test_json(self):
#YOUR CODE HERE
return json.dumps({'json':True})
@http.route('/test/http/method/', auth='none', type='http', website=True)
def test_http(self):
#YOUR CODE HERE
return json.dumps({'http':True})
使用上面介紹的方法之一(簡要地)調用模板中的方法來放置此javascript。
<script>
jQuery.get('/test/http/method/',function(data){ console.log("HTTP RESPONSE: " + data) });
</script>
<script>
jQuery.ajax({
type: "POST",
url: '/test/json/method/',
dataType: 'json',
async: true,
data: JSON.stringify({}),
contentType: "application/json; charset=utf-8",
success: function (data) {
console.log("JSON RESPONSE: " + JSON.stringify(data));
},
failure: function(data){
console.log(JSON.stringify(data));
}
})
</script>
當涉及到Odoo中的路由時,需要學習很多東西。有安全問題,您應該小心的權限問題。
https://www.odoo.com/documentation/8.0/howtos/website.html討論幾個概念的快速概述。 https://www.odoo.com/documentation/8.0/reference/http.html討論控制器。 https://www.odoo.com/documentation/8.0/reference/javascript.html涵蓋了javascript。除此之外,在網頁上看看,更多的例子,在控制器目錄中的網站插件。
你必須提供一個實例:'someinstance.thisMethod()' –
我怎樣才能爲控制器類創建一個對象? – KbiR