栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

django报错:TypeError: context must be a dict rather than set.

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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}

运行成功:

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/303909.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号