django报错:TypeError: context must be a dict rather than set.
最近开始学习《python编程:从入门到实践》这本书,其中django 部分因为版本不同,照抄书中的代码并不能顺利运行,有很多需要改动的地方;
TypeError: context must be a dict rather than set.
运行服务器后,报错,提示“context必须是字典”,context是书中在view.py中定义topics(request)时,用来给模板发送信息的“上下文”
因此,检查view.py:
from django.shortcuts import render
# 导入函数render,根据试图提供的数据渲染响应
from .models import Topic
# 引入与所需数据相关的模型topic
# Create your views here.
def index(request):
# 学习笔记的主页
return render(request, 'learning_logs/index.html')
def topics(request):
# 显示所有主题
topics = Topic.objects.order_by('date_added')
context = {'topics:', topics}
return render(request, 'learning_logs/topics.html', context)
发现倒数第二行,‘topics:’把冒号输在了引号里面,导致context不是一个字典,
改为:
context = {'topics': topics}
运行成功:
————————————————
版权声明:本文为CSDN博主「neo_niu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/neo_niu/article/details/120677272