web开发常见异常与处理方式(web框架_django)
| 框架类型 | 报错信息 | 错误类型 | 处理方式 |
| Django |
AttributeError: 'WSGIRequest' object has no attribute 'session' |
session未正常启用,就对session进行了调用。常见于,自定义中间件注册顺序不合适导致 | django默认是通过中间件启用session服务的。将自定义调用session的中间件或功能组件,放在启动session的中间件执行流之后调用执行即可。 |
| Django |
AttributeError: 'QuerySet' object has no attribute '_meta' |
对象调用有误,或者类型错误。当前是用QuerySet对象,进行的对应方法的调用。 | 查看具体,调用当前方法的对象,是否为目标对象。 |
| Django |
ValueError: ModelForm has no model class specified. |
ModelForm在Meta类中配置信息未指定具体的model信息。 | |
| Django |
RuntimeError: cryptography is required for sha256_password orcaching_sha2_password |
mysql版本身份验证出现问题引起的 |
1). ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则 2).ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码 3).FLUSH PRIVILEGES; #刷新权限 4).再重置下密码:alter user 'root'@'localhost' identified by '123456'; 参考文章来源 原文链接:https: //blog.csdn.net/p_xiaobai/article /details/85334875
|
|
RuntimeError: You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to |
url路由中,以 " / "结尾,请求中未添加 |
路由中结尾可以不加 " / ",django的路由组件会自动添加 |
|
|
AttributeError: 'str' object has no attribute 'decode' |
解决方式一,修改源码,从报错的连接中点进去,把if语句注释掉,或者把decode改成encode。修改源码,慎用。 | ||
|
django.db.utils.OperationalError: (1045,"Access denied for user 'root'@'localhost' (using password: YES)") |
数据库连接基本信息错误 | 查看settings中的设置,用户名,密码等信息是否正确 | |
|
ValueError: Database returned an invalid datetime value. Are time zone definitions for your database installed? |
时间配置信息冲突 |
确认是否是用的UTC时间,如果不是用的UTC时间,将USE_TZ = True,设置为False或者注释掉 当启用USE_TZ = True 时,要做到如下设置: 在使用时间参数时,确保时间已经转换成 UTC ;
|
|
|
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include()without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead. |
Django版本不同导致的异常。当路由分发中,如果是运行的django2.0版本,设置名称空间时,格式如下,re_path(r'^blog/',include('blog.urls', namespace='blog')),就会报当前错误。但同样的设置在django1.0版本中会支持,原因前后版本的处理函数及参数要求不一致 |
按照提示,给include传入参数的时候,传入 ((url,app_name),namespace=***)格式的一个元组 | |
|
django.core.exceptions. ImproperlyConfigured: The SECRET_KEY setting must not be empty. |
在配置文件中,出现了import不合法的引用。 | 一般情况下,不会再settings中使用import导入不必要的引用,删掉响应的引用内容即可。 | |
| django.core.exceptions.FieldError: Related Field got invalid lookup: title | 关联字段引用错误 | orm或者视图函数中,对关联字段的引用错误,这里以报错title为例,引用的title是有问题的 | |
| Internal Server Error: | 内部错误 | 查看引用的模块是否正确,是否在变量命名时,有冲突情况 | |
|
No module named _sqlite3 |
创建django项目过程中,发现没有安装sqlite3相关组件 |
安装sqlite,yum -y install sqlite3, 运行./configure, 重新编译python3,make, 重新安装,make install
|
|
| django.template.exceptions.TemplateDoesNotExist: rest_framework/api.html | 使用drf框架时,必须将rest_framework在settings的apps中进行注册,否则django引用不到 | 项目目录的settings中,
INSTALLED_APPS列表中, |
|
|
|
|||