django遇到的那些古怪问题
AssertionError: .accepted_renderer not set on Response
出错原因,没有在合法的方法内使用 response 响应,之前在dispatch内直接return 了,导致报错,return response 必须放在 get post 等方法内。
按道理讲,django视图会先经过dispatch分发,如果我们在分发的时候发现权限问题,那么提前响应应该没问题,那么为啥会报错呢?
报错信息显示accepted_renderer没有在Response内设定,找一下源码,如下(具体原因都在下面代码后面的注释里了):
def dispatch(self, request, *args, **kwargs):
"""
`.dispatch()` is pretty much the same as Django's regular dispatch,
but with extra hooks for startup, finalize, and exception handling.
"""
self.args = args
self.kwargs = kwargs
request = self.initialize_request(request, *args, **kwargs)
self.request = request
self.headers = self.default_response_headers # deprecate? try:
self.initial(request, *args, **kwargs) # Get the appropriate handler method
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(),
self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed response = handler(request, *args, **kwargs) # 这里发现了,请求响应是在dispatch方法内部完成的,响应也是dispatch代为返回的,到这里也没问题,问题在下面。 except Exception as exc:
response = self.handle_exception(exc) self.response = self.finalize_response(request, response, *args, **kwargs) # 在响应之前,dispatch对视图方法返回的响应又加工了一次
return self.response def finalize_response(self, request, response, *args, **kwargs):
"""
Returns the final response object.
"""
# Make the error obvious if a proper response is not returned
assert isinstance(response, HttpResponseBase), (
'Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` '
'to be returned from the view, but received a `%s`'
% type(response)
) if isinstance(response, Response):
if not getattr(request, 'accepted_renderer', None):
neg = self.perform_content_negotiation(request, force=True)
request.accepted_renderer, request.accepted_media_type = neg response.accepted_renderer = request.accepted_renderer # 这里,Response 会定义一个属性 accepted_renderer,也就是说,视图响应的不仅仅是一个 Response,而是加工过的Response,所以如果在dispatch内直接响应Response,就是跳过了后面加工哪一步,就会报错。
response.accepted_media_type = request.accepted_media_type
response.renderer_context = self.get_renderer_context() for key, value in self.headers.items():
response[key] = value return response
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
出错原因,两个,第一,相关包没有安装或者版本有问题;第二,在不合适的地方使用了项目的model模型,版本不同情况不一样。
XXX doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported
出错原因,在不合适的地方导用了model,如本次是在 app.__init__.py中导入本app的model导致的
虚拟环境中,Python库的安装直接使用pip安装,不用加sudo,不然项目没有权限使用该库,会导致出现 Apps aren't loaded yet.
django遇到的那些古怪问题的更多相关文章
- Django QuerySet API文档
在查询时发生了什么(When QuerySets are evaluated) QuerySet 可以被构造,过滤,切片,做为参数传递,这些行为都不会对数据库进行操作.只要你查询的时候才真正的操作数据 ...
- django的queryset和objects对象
1. queryset是查询集,就是传到服务器上的url里面的内容.Django会对查询返回的结果集QerySet进行缓存,这里是为了提高查询效率. 也就是说,在你创建一个QuerySet对象的时候, ...
- 异步任务队列Celery在Django中的使用
前段时间在Django Web平台开发中,碰到一些请求执行的任务时间较长(几分钟),为了加快用户的响应时间,因此决定采用异步任务的方式在后台执行这些任务.在同事的指引下接触了Celery这个异步任务队 ...
- 《Django By Example》第四章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:祝大家新年快乐,这次带来<D ...
- django server之间通过remote user 相互调用
首先,场景是这样的:存在两个django web应用,并且两个应用存在一定的联系.某些情况下彼此需要获取对方的数据. 但是我们的应用肯经都会有对应的鉴权机制.不会让人家随随便便就访问的对吧.好比上车要 ...
- Mysql事务探索及其在Django中的实践(二)
继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...
- Mysql事务探索及其在Django中的实践(一)
前言 很早就有想开始写博客的想法,一方面是对自己近期所学知识的一些总结.沉淀,方便以后对过去的知识进行梳理.追溯,一方面也希望能通过博客来认识更多相同技术圈的朋友.所幸近期通过了博客园的申请,那么今天 ...
- 《Django By Example》第三章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:第三章滚烫出炉,大家请不要吐槽文中 ...
- 《Django By Example》第二章 中文 翻译 (个人学习,渣翻)
书籍出处:https://www.packtpub.com/web-development/django-example 原作者:Antonio Melé (译者注:翻译完第一章后,发现翻译第二章的速 ...
随机推荐
- USACO2002 Open:雄伟的山峦
简要题意: 奶牛们在落基山下避暑,从它们的房子向外望去,可以看到N 座山峰构成的山峦,奶牛发现每座山峰都是等腰三角形,底边长度恰好是高度的两倍.所以山峰的顶点坐标可由两个底部端点求出.设i 座第山峰的 ...
- .Net中字典的使用
/// <summary> /// 获取用户市信息 /// </summary> /// <param name="CustomerId">&l ...
- Convolution & Pooling exercise
convolution First, we want to compute σ(Wx(r,c) + b) for all valid (r,c) (valid meaning that the ent ...
- 开发板ping得通本地,但是不能上网
在3531D的开发板上面接入LAN7500的USB转以太网口,加载完驱动后. 然后开启eth1 查看一下ifconfig,发现没有ip,配置一下ip 直接ping本地发现可以ping得通,代表链路是连 ...
- 分享《Python 游戏编程快速上手(第3版)》高清中文版PDF+高清英文版PDF+源代码
通过编写一个个小巧.有趣的游戏来学习Python,通过实例来解释编程的原理的方式.14个游戏程序和示例,介绍了Python基础知识.数据类型.函数.流程控制.程序调试.流程图设计.字符串操作.列表和字 ...
- 兼容IE浏览器的canvas画线和圆圈
1.新建test.html文件,代码如下: <!DOCTYPE html><html><head> <meta charset="utf-8& ...
- HDU 2886 Lou 1 Zhuang
思维好重要.. 对于n+m == k , 当n == m || abs(n-m) == 1 时n*m取得最大值. 设 s = x*(l-x),s = lx-x^2.其导函数为 s' = -1/2x + ...
- Dynamics CRM 2015/2016 Web API:Unbound Custom Action 和 Bound Custom Action
今天我们再来看看Bound/Unbound Custom Action吧,什么是Custom Action?不知道的小伙伴们就out了,Dynamics CRM 2013就有了这个功能啦.和WhoAm ...
- git 版本管理工具说明
$ git init (初始化本地仓库,会生成.git 文件夹 .git 文件夹里存储了所有的版本信息.标记等内容) $ git add . ...
- LuoguP4016 负载平衡问题(费用流)
题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...