Hello world. Today I got happy felling but i don't where it come from? Lol just for laugh. So today i am gonna share about controller in Odoo. Actually for the first time i hear that, i don't know what should i do? okay in this time, i will try to make definition from myself, you can make your definition too on the comment bellow, we make definition together.
Controller is class on Odoo that can use for us when we want to make link on website, and then show anything that what we want to show on your website. I hope like that, so i am not give you wrong way. So how to create controller on Odoo? Lets see.
This is my class controller, my function on cons.py file :
import logging from openerp import SUPERUSER_ID from openerp import http from openerp.http import request from openerp import models, api class website_qty(http.Controller): @http.route(['/page/dedy'], type='http', auth="public", website="True") def index(self, **post): wcl = [] dedy = request.env['dedy.yuristiawan'] dy = dedy.search([], state='draft') return request.render('dedy_controller.index_assets_frontend_view', { 'dedy': dy, })
As you can see, i can make conclution for this code :
1. Name of the class, you can create name anything what you want, but in this class we need to declaration first. For declaration you can use this code.
http.Controller
2. Name of the function, as you can see you can create name anything what you want, but in the above of name function you need to declaration your url, some thing like this.
@http.route(['/page/dedy'], type='http', auth="public", website="True")
3. Return , actually for return its not really need similar with on python, but in controller you need to render for show data to xml view. something like this.
return request.render('dedy_controller.index_assets_frontend_view', { 'dedy': dy, })
dedy_controller is my module, index_assets_frontend_view is my id in xml file, and then the important id dedy is variable that we want to show in xml file or on website.
4. Inside of the function you can do anything what you want. like in my code, i just show data from my model dedy.yuristiawan, and then i set in return on variable dedy.
5. Just it.
And this one its my cons.xml file :
<template id="index_assets_frontend_view"> <title>Test Input</title> <t t-call="website.layout"> <t t-set="title">Dedy Yuristiawan</t> <div class="oe_structure"> <div class="container"> <t t-foreach="dedy" t-as="value"> <td class="brctbl-col4 js_loc"> <p> <t t-esc="value.name"/> </p> </td> </t> </div> </div> </t> </template>
As you can see, i can make conclution for this code :
1. Template id, its really important for controller, cause usually function on controller bring value to xml with calling template name. See my conclution cons.py file number three.
<template id="index_assets_frontend_view">
2. As you can see i do looping with variable dedy, this variable is from function controller that we set on return. You can see, my conclution cons.py file number 3.
<t t-foreach="dedy" t-as="value">
t-as, it means you can you put value dedy to variable value.
3. And last you had variable, you can do anything, show anything to website, with qweb sintax Odoo. like this looping, show field.
looping :
<t t-foreach="dedy" t-as="value">
show value :
<t t-esc="value.name"/>
4. Actually you can make link on xml code, you can use action on form or just (a href) and link will be the call controller function python.
<a t-att-href="'/page/dedy_next_view/" name="dedy_next_id" class="btn btn-default">Go to next link</a>
action="/page/test_input_view_form" method="post"
5. Last one, if you want to make post method, you need to add input like this one.
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
This is for security to protect our website, that we need get token from Odoo. This is rule for the Odoo 9.
That's it, you can write comment bellow if you still confuse about my code. i hope this can help you little bit. Ahha
Very good.
ReplyDeleteOdoo partner
ReplyDeleteThanks for sharing this informative article. This content is very useful.
Odoo Partners
hi,
ReplyDeletecan we return specific form view of model through controller if yes how should i do that.can you please explain for me.
Hey buddy!! What amazing and useful information you are sharing here, thanks for sharing. I would love to share this information on mine post also so that the visitors of my blog also get a chance to become familiar with this information.
ReplyDeleteOdoo Development Company India |
Hire Odoo developers India
Great informative article! Thanks for sharing this informative article. Looking for the best IT consulting company in Australia? Reach IDYA Technology.
ReplyDeleteNice blog. Thank you for sharing.
ReplyDeleteAndroid App Development Company in India
Hire Android App Developer