我错过了哪些模板标签?

时间:2011-09-20 09:45:44

标签: django-templates

def _simple_report_topic(self,section=None, visit_id=None, primary=''):

    status = []
    report_buckets = {}

    cvm = Cvm(visit_id=visit_id)
    ns = cvm.get_cvm_namespace()
    vmd = ns['vmd']
    assoc_instances = vmd.assoc_instances     

    all_factoids = []
    for ad in section['factoids']:
        all_factoids.extend(self.get_factoids(ad))

    for rt in self.report_topics:

        factoids = [f for f in all_factoids if f.report_topic and \
                    f.report_topic.ad == rt['ad'] and \
                    f.ad not in self.global_seen_factoids]

        # Figure out if we have data for this factoid.
        prs = PatientResponse.objects.filter(
            visit__id=visit_id,
            confirmation__in=self.valid_states,
            factoid__in=factoids,
            feature__ad=section['feature'],
            norm_value__gte=section['threshold'],
        ).order_by('-norm_value')

        if not prs:
            continue

        fv_ad = prs[0].feature_value
        # This flag controls when we show the title for this report topic.
        suppress_label = False

        render_data = []

        if not suppress_label:
            render_data.append(widgets.TopicTitleWidget(title=rt['text']))
            suppress_label = True

        for pr in prs:

            self.global_seen_factoids.add(pr.factoid.ad)
            # The result of the rendering will be a FactoidWidget.
            result = self.render_factoid(factoid=pr.factoid, visit_id=visit_id, primary=primary)
            if result:
                render_data.append(result) 

        if report_buckets.get(fv_ad):
            report_buckets[fv_ad].append(render_data)
        else:
            report_buckets[fv_ad] = [render_data]

    report_buckets = report_buckets.items()
    report_buckets.sort(lambda x,y: cmp(y[0], x[0]))

    # Number the topics.
    counter = 1
    for key, value in report_buckets:
        for each_list in value:
            for item in each_list:
                if item.widget_type == 'TopicTitle':
                    item.title = '%s) %s' % (counter, capfirst(item.title))
                    counter += 1


    if not report_buckets:
        #print "report buckets are %s" % report_buckets
        #print "vmd.m_pmh is %s" % vmd.m_pmh

        if vmd.m_pmh == 'done':
            status.append({
               'text':"No current issues were identified." ,
                'weight':2
            })
        elif vmd.m_pmh == "running":
            status.append({
                'text':'Section started, but not completed.',
                'weight':2
            })
        else:
            status.append({
                'text':'No data collected',
                'weight':2,
            })

    for t1 in status:
        t1['weight'] = string.zfill(str(t1['weight']), 4)
    #print " value of report_buckets is %s" % report_buckets
    #print " value of status is %s" % status
    #print "value of report_buckets %s " % report_buckets
    c = {'status':status}
    print "################################################"
    print c
    #return  c
    #return report_buckets
    return {
             '_report_buckets':report_buckets,
             'status':c,
    }

我得到c的值为:

{'status': [{'text': 'Section started, but not completed.', 'weight': '0002'}]}

我想显示文本的值,所以我在模板上写了

{% load text_tools %}

Why are you not getting printed ?
{% ifequal this_section.type 'simple-report-topic' %}
    {% if status %}
        {% for group in status %}
            {% for score in group %}
                {{ score.text|capfirst }};
            {% endfor %}
        {% endfor %}
    {% endif %}
{% endifequal %}

正在显示

你为什么不打印? 小号 小号

为什么它不进入循环?我缺少什么模板标签?

1 个答案:

答案 0 :(得分:1)

查看:

您不需要c = {'status':status}代替

return {'_report_buckets':report_buckets,'status':status}

<强>模板:

{% ifequal this_section.type 'simple-report-topic' %}
    {% if status %}
        {% for score in status %}
              {{ score.text|capfirst }};
        {% endfor %}
    {% endif %}
{% endifequal %}

请不要急,请阅读您在基础知识上出错的代码。

修改

您的代码正在尝试迭代dict这是不对的。您只需在模板中使用{% for group in status.status %}

即可使代码生效