【问题标题】:django-tables2 : Error during template renderingdjango-tables2:模板渲染期间出错
【发布时间】:2017-01-31 15:33:47
【问题描述】:

我正在编写一个最简单的 django-table2 示例 它失败了 Error during template rendering In template /myproject/app/templates/test_table.html, error at line 2 任何线索这里有什么问题。只是跟随文件。

上下文

1 {% 从 django_tables2 加载 render_table %}

2 {% render_table table %}

3

model.py

​​>
class TestTable1(models.Model):
    col1 = models.CharField(max_length=1, primary_key=True)
    col2 = models.FloatField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'test_table1'

class TestTable1Tbl(tables.Table):

    class Meta:
        model = TestTable1
        attrs = {"class":"paleblue"}

views.py

def test_table(request):

    table = TestTable1Tbl(TestTable1.objects.all())
    RequestConfig(request).configure(table)
    return render(request,"test_table.html",{'table':table})

模板

$ 更多 test_table.html

{% load render_table from django_tables2 %}
{% render_table table %}

以下是错误信息

在处理上述异常期间(无法将字符串转换为浮点数:'table'),发生了另一个异常:

        return template.render(context.flatten()) 

【问题讨论】:

  • 以下是错误信息:在处理上述异常期间(无法将字符串转换为浮点数:'table'),发生另一个异常:
  • 请分享您使用的python、django、django-tables2的版本。
  • 刚刚尝试在 django-tables2 测试套件的测试中重现此错误,但它通过了整个构建矩阵,在这里测试:gist.github.com/jieter/1173ba26dfbd883a0eb1c44b516f155b
  • python 版本 3.5.0 django 版本 1.10 django-tables 版本 1.2.6

标签: django django-tables2


【解决方案1】:

问题出在 table.html 文件上。复制到/templates/django_tables/下后问题解决,谢谢。

【讨论】:

    【解决方案2】:

    我也有同样的问题,但上面的答案一开始让我很困惑。

    table.html 文件似乎是 render table 期望找到的默认模板。我发现专门设置 template_name 并将django_tables2 添加到文件路径解决了问题,而无需从 django2 源复制文件。

    所以适用于上面的例子(我也选择boostrap.html而不是table.html

    class TestTable1Tbl(tables.Table):
    
        class Meta:
            model = TestTable1
            attrs = {"class":"paleblue"}        
            template_name = 'django_tables2/bootstrap.html'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-21
      • 2017-07-03
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 2017-11-13
      • 2020-08-04
      相关资源
      最近更新 更多