1

我有数据库中的城市列表,并希望在 Web 控制器中显示它们的特定数据。在这里,附上我使用的屏幕截图和代码。

Controllerrs.py 文件

from odoo import http

class CurrentWeather(http.Controller):
    @http.route('/current_weather/', auth='public', website='True')
    def index(self, **kw):
        location = http.request.env['weather_location.weather_location']
        return http.request.render('current_weather.index', {
            'locations': location.search([])
            })

    @http.route('/current_weather/condition', auth='public', website='True')
    def index_current_condition(self, **kw):
        weather_condition = http.request.env['current_weather.current_weather']
        return http.request.render('current_weather.index_condition', {
            'weather_conditions': weather_condition.search([])
            })

模板.xml 文件

        <template id="index">
        <t t-call="website.layout">
            <t t-set="title">Locations</t>
            <div class="oe_structure">
                <div class="container">
                    <t t-foreach="locations" t-as="location">
                        <p>
                            <a t-attf-href="/current_weather/condition">
                            <center><t t-esc="location.name"/></center>
                            </a>
                        </p>
                    </t>

                </div>
            </div>
        </t>
    </template>

在此处输入图像描述 当点击孟买时,只有孟买的数据会显示如何做到这一点?目前,所有城市显示的数据都是相同的。

4

1 回答 1

0

在您的控制器index_current_condition中,您需要考虑一种weather_condition基于单击的城市来过滤的方法。这可以通过searchORM API 的方法来完成。

weather_condition.search([])您传递一个空白列表时,您将返回在您的模型中找到的所有记录。所以在这里你需要使用类似的东西weather_condition.search([('city', '=', current_city)]),其中city需要是你模型中的一个字段current_weather.current_weather(并且应该链接到你的其他模型weather_location.weather_location)并且current_city必须以某种方式动态传递给控制器​​。

于 2018-03-28T12:07:37.837 回答