简体   繁体   中英

How to render data from the external model in divs of a view?

I need your help, I have some divs in a view in odoo 13, I need to bring the data from an external model "maintenance.stage" render them in those divs, can someone give me an example of how to do it with a controller to bring those logs and be able to display them in those divs? every time i add a new record it should render in those divs.

enter image description here

enter image description here

After having identified the original controller based on the url of your page (by searching your-page-url in all addons files): in my example here: WebsiteEventSaleController

In your custom module: create your controllers/main.py file:

    
    #import the original controller containing your-page-url:
    from odoo.addons.website_event_sale.controllers.main import WebsiteEventSaleController

    #inherit the original controller:
    class WebsiteEventSearchController(WebsiteEventSaleController):

        #override the def (here: def events) which is associated with your-page-url and which renders the corresponding view:

        @http.route(['/your-page-url', '/page/<int:page>'], type='http', auth="public",
                website=True, csrf=False, sitemap=sitemap_event)
        def events(self, page=1, **searches):
            
            #Get the existing values rendered by the original def (events) locates in the original Controller:   
            res = super(WebsiteEventSearchController, self).events(page=1, **searches)
            values = res.qcontext
            #content example: values = {  'current_date': current_date, 'current_country': current_country, }
        
            #search the maintenance.stage records using a query on your model 
            maintenance_stage_ids = request.env['maintenance.stage'].search([('website_id', '=', 1)])
        
            #add your new maintenance.stage-dictionnary as new key-value pair in the values-dictionary
            values["maintenance_stage_ids"]= maintenance_stage_ids

            return request.render("website_event.index", values)

In your __manifest__.py file: add this line to load the original module ('website_event') which contains the original rendered view "website_event.index":

    'depends': ['website_event'],

After having find the view (here: website_event.index) which is rendered by your controller def (above).

In your view file (here: templates.xml), inherit this view using the module name where the original view is located (here: website_event) and using xpath: hook the ul containing the li-items to add your new items (provided by the rendered values["maintenance_stage_ids"]):

    <template id="index_inherit" inherit_id="website_event.index">
        <xpath expr="//ul[hasclass('o_wevent_index')]" position="inside">      
           <t t-foreach="maintenance_stage_ids" t-as="maintenance_stage">
                <li class="nav-item ">
                 <span t-esc="maintenance_stage['name']" class="badge"/>
                </li>
            </t>
        </xpath>
    </template>

I hope this helps, This is a controller exemple:

class ControllerExemple(http.Controller):
    @http.route('/path/', auth='public', type='http', 
            methods=["GET","POST"], website=True)
    def function_exemple(self):
        obj = request.env['maintenance.stage'].browse(<id>)
        return request.render("module.your_view_id", {'object': obj})

There is a exemple view:

    <template id="your_view_id" name="Something">
        <t t-call="website.layout">
            <div class="container">
                <div><span t-out="object.the_field_you_need"/></div>
            </div>
        </t>
    </template>

It seems that you have several values to display, so use the foreach in your template:

    <template id="your_view_id" name="Something">
        <t t-call="website.layout">
            <t t-foreach="object.the_field_you_need" t-as="value">
                <div class="container">
                    <div><span t-out="value"/></div>
                </div>
            </t>
        </t>
    </template>

https://odoo-master.readthedocs.io/en/master/reference/qweb.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM