【问题标题】:Django-tables2: ValueError at /interactive_table/ Expected table or queryset, not strDjango-tables2:/interactive_table/ 预期表或查询集处的 ValueError,而不是 str
【发布时间】:2016-09-08 16:38:43
【问题描述】:

我正在关注 Django-tables2 教程(可在此处找到:https://django-tables2.readthedocs.io/en/latest/pages/tutorial.html)。到目前为止,我已经修复了所有错误,但我遇到了一个我无法解决的错误。它说我的代码需要一个表或查询集,而不是字符串。

我环顾四周,这个问题的所有解决方案都归咎于版本过时,但我已经更新了它,我仍然得到这个错误。

有人知道我做错了什么吗?

这是我的views.py:

from django.shortcuts import render
from interactive_table import models

def people(request):
    return render(request, 'template.html', {'obj': models.people.objects.all()})

这是我的models.py:

    from django.db import models

class people(models.Model):
    name = models.CharField(max_length = 40, verbose_name = 'Full Name')

这是我的模板.html:

{# tutorial/templates/people.html #}
{% load render_table from django_tables2 %}
<!doctype html>
<html>
    <head>
        <link rel="stylesheet" href="{{ STATIC_URL }}django_tables2/themes/paleblue/css/screen.css" />
    </head>
    <body>
        {% render_table people %}
    </body>
</html>

【问题讨论】:

  • 您将“obj”返回到模板,而不是“人”

标签: python django django-tables2


【解决方案1】:

在渲染函数中将obj 更改为people

尝试了解模板和模板变量如何与 django 一起使用。

文档可能是look的好地方

【讨论】:

    【解决方案2】:

    将您的模板响应更改为返回 people 而不是 obj

    return render(request, 'template.html', {'people': models.people.objects.all()})
    

    【讨论】:

      猜你喜欢
      • 2020-06-17
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 2020-10-25
      • 2021-04-24
      • 2021-10-09
      • 2021-12-19
      • 2023-04-10
      相关资源
      最近更新 更多