This question has been flagged

I have questions for experienced developers:

I am trying to create new custom web controller route. But I do find some troubles here. Let me put down the example. Image this situation:

custom controller:

@http.route(['/<path:custom_path>/'], type='http', auth='public', website=True)
def render_new_template(self, **post):

is conflicting with basic controller:

@http.route('/', type='http', auth="public", website=True, sitemap=True)
def index(self, **kw):

The effect of above code is:

  • when I request website.com/custom_path/ URL - it works perfectly and renders my custom template
  • when I request a core page website.com/page URL - it starts to render it with my new custom template, which is not the effect I want

How to deal with routing in such situation? I do really want to have the same short URLs without additional path like following:

/additional_path/<path:custom_path>/

or even short one:

/c/<path:custom_path>/

I did some research as odoo web controller routing is based on werkzeug, so I found some recommendations but neither was precise enough for Odoo.

The following solutions were mentioned:

  • do it with custom converter
  • do it with Importing werkzeug.routing, creating own map implementation, changing werkzeug.routing.Map to own implementation
  • use "url_for" (probably only in case of Flask) which allows you to point to the

app.py:

app.route('/<path:pattern1>')
app.route('/<path:pattern1>/<path:pattern2>')
def catch_all(pattern1, pattern2=None):
return render_template('template.html', p1=pattern1, p2=pattern2)

app.route('/test')
def test_routing:
args = {'pattern1': 'Posts', 'pattern2': 'create'}
return render_template('test.html', args=args)

test.html:

click here

I know there is a url_for functionality in odoo but it is not doing exactly the same - it is pointing to the path not to the method of this route

I will be really grateful for help, and donate any guy that will help, explain and suggest kind of wise and pretty solution for my case.


Avatar
Discard

You can get some idea of odoo controllers: https://learnopenerp.blogspot.com/search?q=controller

Best Answer

A bit late, but nonetheless. I have been struggling with this issue a lot (especially with multi websites). I found a work-around which works, but probably is not ideal.

Custom Controller:

from odoo.http import request
from odoo.addons.website.controllers.main import Website

class Main(Website):

    @http.route('/<name>', auth='public', website=True)
    def custom_route(self, name, **kwargs):
        if name == 'custom_path':
        # Business logic here
    return request.render(
                'template', {
                    'key' : value,
                }
            )
        return super(Website, self).index(**kwargs)

If the path meets a specified criteria ('custom_path' in above example) use the custom route and render whatever it needs to render, otherwise use the default route.

I hope this answers your question.

Avatar
Discard

Jort, Thanks for sharing; having a simple example that works really helped me.