Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django-无法将字典更新序列元素#0转换为序列_Python_Django - Fatal编程技术网

Python Django-无法将字典更新序列元素#0转换为序列

Python Django-无法将字典更新序列元素#0转换为序列,python,django,Python,Django,我正在尝试检索一些存储在模型中的数据 型号: class Contact(models.Model): name = models.CharField(max_length=20) number = models.IntegerField() slug = models.SlugField() def __unicode__(self): return self.name 网址: 观点: from models import Contact

我正在尝试检索一些存储在模型中的数据

型号:

class Contact(models.Model):
    name = models.CharField(max_length=20)
    number = models.IntegerField()
    slug = models.SlugField()

    def __unicode__(self):
        return self.name
网址:

观点:

from models import Contact

def index(request):
    contacts = Contact.objects.all()[:5]
    return render(request, 'about/home.html', contacts)

def all_contacts(request):
    contacts = Contact.objects.all()
    return render(request, 'about/all.html', contacts)
模板:

{% for contact in contacts %}
    {{ contact.name }} | {{ contact.number }}
{% endfor %}
我得到这个错误:

/
处的TypeError无法将字典更新序列元素#0转换为 序列

这个错误是关于什么的? 我的代码怎么了?
谢谢。

来自文档:

可选参数

上下文

要添加到模板上下文的值字典。默认情况下,这是一个空字典。如果字典中的值是可调用的, 视图将在呈现模板之前调用它

因此:


除了像@Bogey Jammer那样显式创建
上下文
, 您可以将
联系人
添加到
上下文
,方法是在dict中将其指定为
呈现
的参数:

def all_contacts(request):
   contacts = Contact.objects.all()
   return render(request, 'about/all.html', {'contacts': contacts})
Django Version: 1.8.2
Exception Type: TypeError
Exception Value:    
cannot convert dictionary update sequence element #0 to a sequence
def all_contacts(request):
    context = dict()
    context['contacts'] = Contact.objects.all()
    context['otherStuffProcessedByTheTemplate'] = …
    # etc…

    return render(request, 'about/all.html', context)
def all_contacts(request):
   contacts = Contact.objects.all()
   return render(request, 'about/all.html', {'contacts': contacts})