How to get data from URL with a controller in Odoo 9

Create under your module a folder controller, then in that folder create a file controller.py and __init__.py then link your new file in __init__.py, don’t forget to link your new folder in the __init__.py at the root folder of your module.

controller.py code bellow will make available the URL www.myodoo.com/page/random/random_variable, random_variable value will be stored in values dictionary on key variable.

import openerp.http as http
from openerp.http import request
import logging
_logger = logging.getLogger(__name__)

class Controller(http.Controller):

    @http.route(['/page/random/<string:variable>'],
                type='http', auth="user", methods=['GET'], website=True)
    
    def view(self, **kwargs):
        values = dict(kwargs)
        object_ids = request.env['model.name'].search([('your_field','=', values['variable'])])
        values['object_ids'] = object_ids
        values['customer'] = object_ids[0].customer_id.name
        return request.render('module.template_id', values)

in folder view of your module create the file template.xml

<odoo>
	<template id="template_id" name="template Name">
		<head>
			<meta charset="utf-8" />
			<meta http-equiv="X-UA-Compatible" content="IE=edge" />
			<meta name="viewport" content="width=device-width, initial-scale=1" />
		</head>
		<body>
                <div class="container">
				<span t-field="variable" />
			</div>
		</body>
	</template>
</odoo>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.