背景
在开发 Django 项目,的时候 原来的版本是1.xx版本,现在直接用 diango3.0 出现的问题:
在直接运行爆出错误:
ImportError: cannot import name 'render_to_response' from 'django.shortcuts'
原因
Django 3.0 已经将 render_to_response 移除了,但是项目中还在使用
解决办法
用render 代替 render_to_response
用途:都是渲染模版用的
不同点:render 方法可接收三个参数,一是request参数,二是待渲染的html模板文件,三是保存具体数据的字典参数。它的作用就是将数据填充进模板文件,最后把结果返回给浏览器。render 自动使用RequestContext,而 render_to_response 需要 coding 进去。
例子:
# from django.shortcuts import render, HttpResponse, render_to_response
from django.shortcuts import render, HttpResponse
注解的是原来的,修改为打开的。
接下来修改使用的地方
#return render_to_response('404.html')
return render(request,'404.html','')
具体遇到的问题可能不一样,但是原理是一样,找到相关的地方修改。
谢谢关注