【问题标题】:Return an extra argument with the datachart使用数据图返回一个额外的参数
【发布时间】:2014-01-14 08:51:00
【问题描述】:

我正在尝试打印图表并将带有数据图表的参数返回到模板。

Views.py:

def errors(request):
    totalErrors = total_errors()
    table = table_errors()
    return render_to_response('macaco_errores.djhtml', {'totalErrors': totalErrors, 'table': table})

没有'table'参数打印图表没有问题,但是当我加载“load_chart”时,我在模板中得到'False'。

编辑: 模板:

{% block head %}
    {% load nvd3_tags %}
    {% include_chart_jscss %}
    {% load_chart charttype totalesData totalesContainer extra %}   
{% endblock %}

{% block body %}
<div align="center">
    <h1>Errors</h1>
    {% include_container totalesContainer 600 1000 %}
</div>

<div align="center">
    <table border="1">
    <tr>
    <th>Log</th>
    <th>Type</th>
    </tr>
    {% for type,log,date in table.items %}
    <tr>
    <td>{{ type }}</td>
    <td>{{ log }}</td>
    </tr>
    {% endfor %}
    </table>
</div>
{% endblock %}

【问题讨论】:

  • 发布模板的来源。听起来您给 nvd3.js 提供了错误的配置,而不是 Django 方面的任何问题
  • 我用模板编辑过。
  • 是的。我正在使用最新的,我可以绘制图表。我的问题是我想在表格(模板内)中打印另一个变量(字典),当我从视图返回两个参数时它没有绘制。

标签: django django-templates nvd3.js


【解决方案1】:

{% load_chart charttype totalesData totalesContainer extra %}

这个标签在上下文中需要四个变量:charttypetotalesDatatotalesContainerextra

您似乎没有从您的视图函数(仅提供totalErrorstable)中传递它们中的任何一个

因此,您发布的代码无法实现这一点。

请参阅 django-nvd3 文档中的此示例,了解您需要从视图函数中提供的上下文数据类型:
http://django-nvd3.readthedocs.org/en/latest/introduction.html#example-how-to-create-a-piechart

另请参阅Django docs,了解如何将变量从视图函数传递到模板进行渲染。

例如,在您看来,您当前正在使用render_to_response helper,它接受一个变量名和值的字典来传递给模板...这些是 only 在模板。

【讨论】:

  • 我知道。事实上,你可以传递你想要的变量。这些变量在views.py 中配置。重点是在 render_to_response 中返回两个参数。如果我只返回一个,用于图表,没有问题。
  • 如果您不从视图中传递变量,模板将无法使用它们
  • 我在views.py中有“return render_to_response('macaco_errores.djhtml', {'totalErrors': totalErrors, 'table': table})”
  • 是的,但您没有传递标签呈现图表所需的其他四个变量
  • 谢谢无熵!!!你给我光。我所做的只是将所有参数“累积”在一个中,并在模板访问它们。
【解决方案2】:

解决办法是:

def counterroresmacaco(request):
    errores = errores_macaco()
    errores['tabla'] = tipoerror_ticker()
    return render_to_response('macaco_errores.djhtml', errores)

'errores' 是一个字典,然后在模板中我使用 for 来迭代 'tabla'。 传递两个参数的问题(正如我所做的那样)是 django-nvd3 无法识别示例中的参数。

{% load nvd3_tags %}
{% include_chart_jscss %}
{% load_chart charttype totalesData totalesContainer extra %}

{% include_container totalesContainer 600 1000 %}

<table border="1">
<tr>
<th>Tipo de log</th>
<th>Ticker</th>
</tr>
{% for reg in tabla %}
<tr>
<td>{{ reg.log }}</td>
<td>{{ reg.ticker }}</td>
</tr>
{% endfor %}
</table>

【讨论】:

  • 你错了,问题不在于你“传递了两个参数”(因为你没有:你传递了一个字典,这是正确的)。问题是 tabla 键不在您的字典中。
  • 在第一个中,我返回了两个参数,一个名为“table”(我知道那不是“tabla”)。也许还有更多的解决方案,但我不得不从两个参数更改为仅在一个中添加所有参数,因为模板中的 load_chart 无法接收其他参数只能接收字典,您必须在 views.py 中对其进行配置,添加键 'charttype totalesData totalesContainer extra' 及其值。
  • 您发布的代码与您所说的无关
猜你喜欢
  • 1970-01-01
  • 2023-03-09
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 2015-06-24
  • 1970-01-01
相关资源
最近更新 更多