首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从查询行中turbogears2乘以价格和成本

从查询行中turbogears2乘以价格和成本
EN

Stack Overflow用户
提问于 2019-01-22 06:29:45
回答 2查看 40关注 0票数 0

我知道这个问题已经被问过了,但我不能真正理解答案背后的想法,因为我是编程的初学者,而且对我来说几乎一切都是新的。

我试图将每种配料的价格与其数量相乘,以获得其成本,然后将所有配料的成本相加,得到配方的final_cost,并在我的html模板上查看它。

我有一个查询,它返回来自DB的键和值的字典,现在im继续执行计算并在html上查看final_cost。

代码语言:javascript
复制
@expose('recipe4.templates.newnew')
    def getTotalCost(self):
        i = Ingredient
        ic = Ingredient_Cost
        ri = Recipe_Info
        r = Recipe
        val = DBSession.query(i.ingredient_name, ic.Unit_of_Measure, ri.quantity, ic.price_K, ic.updated_at).filter \
        (r.recipe_name == "pancake",
         r.recipe_id == ri.recipe_id,
         ri.ingredient_id == i.ingredient_id,
         i.ingredient_id == ic.ingredient_id)

        dict(entries=val)
        final_cost=0
        for k,v in entries:
            price=entries.price_K
            qty=entries.quantity
            cost=price*qty
            final_cost+=cost

        return final_cost
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-15 06:40:50

在@amol和进一步研究的帮助下,我终于解决了自己的问题,这对我很有帮助。

代码语言:javascript
复制
sub_cost=[ ]    
final_cost=[ ]  
for k in val:  #iterate through each row of tuples in the returned db object
    for v in k:  #iterate through each values of individual tuples
        price=k[3] #position of price in tuple
        qty=k[4]
        cost=price*qty
        sub_cost.append(cost)
        break #breaks the loop
final_cost.append(sum(sub_cost))
return dict(final_cost=final_cost)
票数 0
EN

Stack Overflow用户

发布于 2019-01-23 09:49:33

我没有一步一步地检查代码,但总体思路似乎是正确的。我看到的主要问题是,您正在公开模板recipe4.templates.newnew,但没有返回字典。

每当您公开一个模板时,控制器操作必须返回一个字典。字典的所有键将作为变量在模板中可用。

因此,如果希望在模板中访问return dict(final_cost=final_cost),则应该执行final_cost操作。

请参阅https://turbogears.readthedocs.io/en/latest/turbogears/templating.html#template-variables

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54302460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档