'How to change Odoo blog url?

I'm developing our corporate website with Odoo (v14) and for the News page, I use default website_blog app/module. I'm able to list the blog posts as news on the page/section that I want. The only thing that I couldn't change is the url.

So, by default, when you create a new post (+ New --> Blog post) it automatically creates the url of the post as:

/blog/our-blog-1/blog-title

blog-title is, obviously, the title that you enter for the post.

our-blog-1 is the default blog and is also editable. Configuration --> Blogs (you can either edit the current ones or create a new one.)

The only thing that I couldn't figure it out is to change /blog/. I want to set this path as /news/ by default. So, the result would be like this:

/news/our-blog-1/blog-title 

I would greatly appreciate if you could help me to solve this.



Solution 1:[1]

The /blog path come from the defined path at file website_blog/controllers/main.py under class WebsiteBlog(http.Controller):. You can do the following to change /blog to /news:

  1. Create your own module inherited from website_blog module.
  2. Define WebsiteBlog class inherited from existing WebsiteBlog.
  3. Redefine def blog with your own route like the following:
from odoo.addons.website_blog.controllers.main import WebsiteBlog


class WebsiteBlog(WebsiteBlog):

    @http.route([
        '/news',
        '/news/page/<int:page>',
        '/news/tag/<string:tag>',
        '/news/tag/<string:tag>/page/<int:page>',
        '''/news/<model("blog.blog"):blog>''',
        '''/news/<model("blog.blog"):blog>/page/<int:page>''',
        '''/news/<model("blog.blog"):blog>/tag/<string:tag>''',
        '''/news/<model("blog.blog"):blog>/tag/<string:tag>/page/<int:page>''',
    ], type='http', auth="public", website=True, sitemap=True)
    def news(self, blog=None, tag=None, page=1, search=None, **opt):
        Blog = request.env['blog.blog']
        if blog and not blog.can_access_from_current_website():
            raise werkzeug.exceptions.NotFound()

        blogs = Blog.search(request.website.website_domain(), order="create_date asc, id asc")

        if not blog and len(blogs) == 1:
            return werkzeug.utils.redirect('/news/%s' % slug(blogs[0]), code=302)

        date_begin, date_end, state = opt.get('date_begin'), opt.get('date_end'), opt.get('state')

        if tag and request.httprequest.method == 'GET':
            # redirect get tag-1,tag-2 -> get tag-1
            tags = tag.split(',')
            if len(tags) > 1:
                url = QueryURL('' if blog else '/news', ['blog', 'tag'], blog=blog, tag=tags[0], date_begin=date_begin, date_end=date_end, search=search)()
                return request.redirect(url, code=302)

        values = self._prepare_blog_values(blogs=blogs, blog=blog, date_begin=date_begin, date_end=date_end, tags=tag, state=state, page=page, search=search)

        # in case of a redirection need by `_prepare_blog_values` we follow it
        if isinstance(values, werkzeug.wrappers.Response):
            return values

        if blog:
            values['main_object'] = blog
            values['edit_in_backend'] = True
            values['blog_url'] = QueryURL('', ['blog', 'tag'], blog=blog, tag=tag, date_begin=date_begin, date_end=date_end, search=search)
        else:
            values['blog_url'] = QueryURL('/news', ['tag'], date_begin=date_begin, date_end=date_end, search=search)

        return request.render("website_blog.blog_post_short", values)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alle Aldine