Skip to Content
Menu
This question has been flagged
2 Replies
12096 Views

Hello,


I am trying to develop a custom homepage for my website but I cannot manage to make it work the way I want.

I would like to display some backend information (events, for example) so I made a controller that gets all that data and makes a render_template afterwards.

My problem is that I can't find a way to call it "directly". My homepage url is like this : http://localhost:8069, so I don't know how to define the @http.route option for my controller.


I tried with an ajax.jsonrpc call, and it works, but I then I cannot edit a part of the template because it crashes. My code was like this :

homepage/xml :

<template id="homepage_template" inherit_id="website.homepage">

        <xpath expr="//div[@id='wrap']" position="inside">

            <div id="events"/>

        </xpath>

    </template>


<template id="wdi_homepage_data">

     event infos here

</template>


And my controller looks like this :

@http.route('/homepage/', methods=['POST'], type='json', auth="public", website=True)

    def load_data(self, **kw):

        events = request.env['event.event'].search([('date_begin', '>', fields.Date.today())], limit=4)

       return request.env['ir.ui.view'].render_template("my_module.homepage_template", {

            'event': events,

        })


Can someone explain me how to make a proper template for the website homepage ?

Thanks a lot !


Avatar
Discard
Best Answer

HI,

<template id="homepage_template" inherit_id="website.homepage">

        <xpath expr="//div[@id='wrap']" position="inside">

            <div id="events"><t t-call='your_module_name.wdi_homepage_data'/>

</div>

        </xpath>

    </template>


Where in wdi_homepage_data add your html templates and try,

Avatar
Discard
Best Answer

Copy paste the controller of the website app. In Odoo 10 it is (look at your 'website' addon controllers):

@http.route('/', type='http', auth="public", website=True)

 def index(self, **kw):

        page = 'homepage'

        main_menu = request.env.ref('website.main_menu', raise_if_not_found=False)

        if main_menu:

            first_menu = main_menu.child_id and main_menu.child_id[0]

            if first_menu:

                if first_menu.url and (not (first_menu.url.startswith(('/page/', '/?', '/#')) or (first_menu.url == '/'))):

                    return request.redirect(first_menu.url)

                if first_menu.url and first_menu.url.startswith('/page/'):

                    return request.env['ir.http'].reroute(first_menu.url)

        return self.page(page)


Just add your code inside this function. Do not remove other stuff and do not forget to import in your controller libraries required by the function above

Avatar
Discard