我是 openERP 的新手,我正在创建一个 web 模块。在这个模块中,我想读取服务器上的文件,在屏幕上显示其内容并使用按钮打印屏幕内容。
我已经创建了打开文件并返回其内容的 python 函数。我制作了一个小部件,显示一个触发打印机调用的按钮。但是,我不知道如何从 JQuery 代码中调用 python 函数。
这是我的模块的外观:
<web_example>
+-- __openerp__.py
+-- __init__.py
+-- web_example.py
+-- web_example_view.xml
+-- static/
+-- src/
+-- js/
+--first_module.js
+-- xml/
+--web_example_view.xml
这是我的python代码:
from osv import fields, osv
import re
class web_example(osv.osv):
_name='web.example'
#_table ='test_printer'
_description = 'Test pour l\'impression de fichiers'
def button_function(self, cr, uid, ids, context=None):
filename = '/home/stage2013/Documents/testlecture'
fichier = open('/home/stage2013/Documents/testlecture', 'r')
toutesleslignes = fichier.readlines()
fichier.close()
print toutesleslignes
return toutesleslignes
web_example()
这是我的 JS 代码:我想要在文件中获取函数 def button_function(...) 的结果。
// static/src/js/first_module.js
openerp.web_example = function (instance) {
instance.web.client_actions.add('example.action', 'instance.web_example.Action');
instance.web_example.Action = instance.web.Widget.extend({
template: 'web_example.action',
events: {
'click .oe_web_example_print button' : 'print_start',
'click .oe_web_example_load button' : 'load_start'
},
init: function () {
this._super.apply(this, arguments);
},
print_start: function () {
window.print();
},
load_start: function () {
//CALL THE PYTHON FUNCTION FROM WEB_EXAMPLE.PY
}
});
};
和 xml (web_example.xml):
<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<record model="ir.actions.client" id="action_client_example">
<field name="name">Example Client Action</field>
<field name="tag">example.action</field>
</record>
<menuitem action="action_client_example" id="test_printer_2" parent="product.menu_products" name="Test impression 2"/>
</data>
</openerp>
模板(static/src/xml/web_example.xml):第一个按钮是在JS中加载文件,第二个是打印页面
<templates>
<div t-name="web_example.action" class="oe_web_example oe_web_example_stopped">
<p class="oe_web_example_load">
<button type="button">Load file</button>
</p>
<p class="oe_web_example_print">
<button name="button_function" type="object" >Imprimer</button>
</p>
<textarea id="response_fichier"></textarea>
</div>
</templates>
谁能帮我从 JS 代码中调用 python 函数?或者告诉我我是否以错误的方式这样做......