【问题标题】:How to get specific data of particular city?如何获取特定城市的具体数据?
【发布时间】:2018-03-28 07:48:08
【问题描述】:

我在数据库中有城市列表,并希望在 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([])
            })

Template.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>

当点击孟买时,只有孟买的数据会显示如何做到这一点? 目前,所有城市显示的数据都是一样的。

【问题讨论】:

  • 感谢您更正代码,但请给我一些解决方案而不是缩进 sir@Vadim Kotov

标签: python controller odoo odoo-11


【解决方案1】:

在您的控制器index_current_condition 中,您需要想办法根据点击的城市过滤weather_condition。这可以通过search method of the ORM API 完成。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2014-08-21
    • 2016-01-31
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多