Python Appengine - 将两个实体传递给jinja模板视图

时间:2017-03-03 15:14:09

标签: python-2.7 google-app-engine jinja2 webapp2

我有一个View-Consult页面,请求处理程序是:

class ViewConsultPage(webapp2.RequestHandler):
    def get(self):
        consult = ndb.Key(urlsafe=self.request.get('key')).get()
        assessment = ndb.Key(urlsafe=self.request.get('key')).get()
        template = JINJA_ENVIRONMENT.get_template('/templates/view-consult.html')
        template_values = {
        'consult': consult,
        'assessment': assessment
        }
        self.response.out.write(template.render(template_values))

显示有关咨询的信息。然后使用与咨询相同的ID创建评估。然后,我想在同一页面上显示有关咨询和评估的信息。

我假设我将评估添加到类似于咨询的处理程序中:

data work.want2;
input Y M $ ID $ volume;
datalines;
2009    JAN A1     100
2009    FEB A1     20
2009    FEB A1     80
2009    JAN A2     100
2009    JAN A2     100
2009    FEB A2     20
2009    FEB A2     80
2009    JAN A3    100
2009    FEB A3    150
2009    MAR A3    100
2011    DEC A1     100
2011    DEC A1     20
2011    DEC A2     20
2011    DEC A3     120
2011    DEC A3     80
2011    OCT A1     100
2011    OCT A2     20
2011    OCT A2     100
;
proc print data=want2;
run;

/*Code 2--> to sum by Y M ID*/
PROC SQL;
create table want3 as SELECT 
Y,
M, 
ID,
sum(volume) AS sumvolume
FROM want2
GROUP BY Y, M ,ID; 
QUIT;
/*Code 3 -->get sum by Y M*/
PROC SQL;
SELECT 
Y,
M, 
sum(sumvolume) AS sumvolume_MO
FROM want3
GROUP BY Y, M; 
QUIT;

当我加载view-consult页面时,Consult属性像以前一样呈现,但是Assessment属性,例如{{assessment.assess_notes}}正在打印为空字符串(或者什么都没有)。

1 个答案:

答案 0 :(得分:1)

consultassessment共享相同的密钥ID,但不是相同的密钥和urlsafe字符串,因此您无法使用相同的方法来获取实体。

您可以通过其密钥ID获取assessment实体 - 两个实体都相同。所以我要像这样重写get()的前两行:

consult_key = ndb.Key(urlsafe=self.request.get('key'))
consult = consult_key.get()
# assessment and consult have the same key ID
assessment = Assesment.get_by_id(consult_key.id())