Web controllers are classes in OpenERP able to catch the http requests sent by any browser. They allow to generate html pages to be served like any web server, implement new methods to be used by the Javascript client, etc.
Controllers File
By convention the controllers should be placed in the controllers directory of the module. Example:
web_example
âââ controllers
â âââ __init__.py
â âââ my_controllers.py
âââ __init__.py
âââ __openerp__.py
In __init__.py
you must add:
import controllers
And here is the content of controllers/__init__.py
:
import my_controllers
Now you can put the following content in controllers/my_controllers.py
:
import openerp.http as http
from openerp.http import request
Controller Declaration
In your controllers file, you can now declare a controller this way:
class MyController(http.Controller):
@http.route('/my_url/some_html', type="http")
def some_html(self):
return "<h1>This is a test</h1>"
@http.route('/my_url/some_json', type="json")
def some_json(self):
return {"sample_dictionary": "This is a sample JSON dictionary"}
A controller must inherit from http.Controller
. Each time you define a method with @http.route()
it defines a url to match. As example, the some_html()
method will be called a client query the /my_url/some_html
url.
Refer Web Controllers
hi all ! i want to upload file in my page, but request cannot get file upload. please tell me why? detail : */ /*line code*/ cgi.FieldStorage(fp=fp, environ=environ) /*cannot get file upload Tks ! my code: /temp = """<html><body>/ /<form method="post" action="/sa/save">/ /<p>File: <input type="file" name="myfile"></p>/ /<p><input type="submit" value="Upload"></p>/ /</form>/ /</body></html>"""/ /class simplecontroller(http.Controller):/ /_cp_path = '/sa'/ /@http./ httprequest [1] /def index(self, req, **kwargs):/ /return temp/ /@http./ httprequest [2] /def save(self, req, **kwargs):/ /environ = req./ httprequest [3] /.environ/ /fp=environ['wsgi.input']/ */fs = cgi.FieldStorage(fp=fp, environ=environ)# page load forever when run to this line/* /f = fs.getfirst('myfile')/ /filepath =req.params.get('myfile')/ /fout = open(filepath,'w') / /fout.write(f.file.read()) / /fout.close() / /return "upload successfully"/ [1] http://terp.vn/forum/tags/httprequest/ [2] http://terp.vn/forum/tags/httprequest/ [3] http://terp.vn/forum/tags/httprequest/