2

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:

<a href="{{url_for('catch_all', **args)}}">click here</a>

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.

Krys.
  • 21
  • 3
  • Firstly: nice answer, needs some formatting, that's all. Welcome to StackOverflow btw. Secondly: yes i know you don't want long URLs but what is with a short one like `/c/custom_path`? It's short and wont interfere with the default controller. – CZoellner Nov 15 '21 at 14:35
  • Very much thank you for editing.. Yes, indeed, I am pretty new here :) it's time to start asking and answering others' questions. I also did not know that the spelling checking is so strict here, from now on I will take care about each sentense ;) Regarding the /c/custom_path - if this was enough, I would not post my question. Unfortunately the demand is to keep it exactly like posted above. /custom_path/ - it's a must, though I hope, someone will help in this particular case. If I found one, a will share it here for others. – Krys. Nov 15 '21 at 16:27
  • Haven't edited it, yet ;-) Okay i'm interested in an answer, too. There are always things to learn :D – CZoellner Nov 15 '21 at 16:40
  • Right @CZoellner, there is always a chance someone has done it before. In the meantime I am working on current sollution. – Krys. Nov 15 '21 at 17:20

0 Answers0