Python 对于';模型#u对象';在';查询集合列表';显示为空。Django 1.11

Python 对于';模型#u对象';在';查询集合列表';显示为空。Django 1.11,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我刚开始使用Django,但在查询集和for循环方面遇到了一些问题 我已经创建了一个模型,并用几个条目填充了我的数据库。在my views.py中,我已将这些条目的数量发送到我的模板,并可以在HTML中看到结果 然而,当我发送这些条目的查询集列表时,我似乎无法访问它们,我不确定为什么 这是view.py # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.shortcuts import rend

我刚开始使用Django,但在查询集和for循环方面遇到了一些问题

我已经创建了一个模型,并用几个条目填充了我的数据库。在my views.py中,我已将这些条目的数量发送到我的模板,并可以在HTML中看到结果

然而,当我发送这些条目的查询集列表时,我似乎无法访问它们,我不确定为什么

这是view.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from website.models import Member
# Create your views here.

def index(request):
    """ View for the homepage of the site """
    num_mem = Member.objects.count()
    mem_list = list(Member.objects.all())
    context = {
            'mem_list': mem_list,
            'num_mem' : num_mem,
    }

    #render the HTML template index.html with the data
    return render(request, 'index.html', context=context)
在index.html中,我尝试访问这些文件

{% extends "base_generic.html" %}

{% block content %}
    {{ num_mem }}
    {% for member in mem_list %}
        There are Members!
    {% empty %}
        Sorry can't find the members =/
    {% endfor %}
{% endblock %}
返回的结果如图所示

“6抱歉,找不到成员=/”

所以我可以看到num_mem值,可以看到当前有6个值

其目的是循环浏览成员列表,并对每个值进行处理,例如

<a href='{{member.m_name.contact_email}}'> Contact {{member.m_name}} here...</a>

我正在使用Django 1.11和Python 2.7.5,提前感谢您的帮助

不要将Member.objects.all转换为列表。它必须返回查询集而不是列表

因此,请从列表中删除该列表

mem\u list=list(Member.objects.all())

一定是这样


mem\u list=Member.objects.all()。不知道为什么这两个都不起作用,因为Django的文档似乎表明应该这样做。我将尝试重现错误,并让您知道:)你好,Cian,我尝试重现错误,但对我来说效果很好。你为什么不尝试更新django?最新版本现在是2.1。嗨,Vineth,我解决了这个问题,mem_列表上有一个命名问题。我想可能是复制粘贴造成的。重写代码解决了这个问题,同时也澄清了问题。因此,我将把这标记为解决问题的答案。再次感谢!
mem_list = Member.objects.all()