不期望 django 查询集响应

Not expected django query set response

我正在使用 django 进行查询集以列出一些课程。问题是当我在 django shell 中进行查询时,它 returns 是这样的:,....]>

如何获取table信息?

PSD:我完全按照我的描述与用户 table 进行了查询集,我得到了预期的结果。但它无法在模板中显示结果。所以如果你能帮忙...提前感谢你的帮助。

class ListCursos( TemplateView):
    model1 = User
    model2 = Course
    template_name = 'plantillas/miscursos.html'

    def get_context_data(self, *args, **kwargs):
        context = super(ListCursos, self).get_context_data(**kwargs)
        context['usuarios'] = User.objects.all()
        context['cursos'] = Course.objects.all()
        return context

模型的每个实例的列值存储为实例变量。你没有提供你的模型的定义,所以我就拿这个来举例。

class Course(models.Model): # example model
    name = models.CharField(max_length=10)
    students = models.IntegerField()

当您有 Course 个模型的查询集时,您可以通过索引

访问它们
>>> all_courses = Course.objects.all()
<QuerySet [<Course: Course object (1)>]>
>>> first_course = all_courses[0]

要访问所选 Course 模型实例的值,您只需键入 class 定义中的列的名称。例如,如果您的 Course 模型具有名称历史和 10 个学生,则

>>> first_course.name # just type the name of the column
'history'
>>> first_course.students
10

所以要在 django 模板中访问它们,考虑到您在上下文中传递 Course.objects.all(),键为 "cursos"。 (就像你在做的那样)

{% for course in cursos %}
    <div>{{course.name}}</div>
    <div>{{course.students}}</div>
{% endfor %}