'Using template variable in odoo

I am new to Odoo. I'm currently using v12 of Odoo. How to render a template variable in XML like it is done in Django?

e.g. views.py

def function_name(self):
    context={
    'something':something
    }
    .
    .
    return render(request, "index.html", context)

e.g. index.html

{% for item in context %}
<p>{{ item.something }}</p>
{% endfor %}

How to render this in Odoo XML and how to pass the context variable from python file?



Solution 1:[1]

In Odoo we can define a dictionary with the required values that you want to use in the template and then pass it to the render method as a second argument with the template name as a first argument. It will render the template with the required values.

I am adding an example for the same problem of how we can achieve the same in Odoo.

values = dict()
values.update({
            'date': date_begin,
            'date_end': date_end,
            'grouped_tasks': grouped_tasks,
            'page_name': 'task',
            'default_url': '/my/tasks',
            'pager': pager,
            'searchbar_sortings': searchbar_sortings,
            'searchbar_groupby': searchbar_groupby,
            'searchbar_inputs': searchbar_inputs,
            'search_in': search_in,
            'search': search,
            'sortby': sortby,
            'groupby': groupby,
            'searchbar_filters': OrderedDict(sorted(searchbar_filters.items())),
            'filterby': filterby,
        })
        return request.render("project.portal_my_tasks", 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