Django常见错误总结
1 ImportError: No module named 'MySQLdb'
解决方法:
. 安装pymysql模块
. 在app的__init__.py文件中写入以下内容
import pymysql
pymysql.install_as_MySQLdb()
2 ImportError: cannot import name 'Thing2Literal' AttributeError: module 'pymysql' has no attribute 'install_as_MySQLdb'
解决方法:
. pip3 uninstall PyMySQL3
. pip3 install -U --force-reinstall pymysql
3 You are trying to add a non-nullable field 'publication_date' to book
without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
解决方法:
这个错误常见于后期修改了model.py文件,在做数据库同步时报错,只要把这个新增字段允许为空即可!
因为默认字段是非空,所以设置字段允许为空(曾尝试设置default="",migrate同步失败)
示例:publication_date = models.DateField(null=True)
4 pymysql.err.InternalError: (1050, "Table 'app01_authordetail' already exists")
解决方法:
因为错误同步,导致正确的表结构无法同步过去,只能删除旧表再同步,适用于测试数据库。如果数据库中有重要的数据库千万别这么干!
. 删除app/migrations/下除__init__.py外所有的py文件;
. 登录数据库删除所有的表;
. 将数据库中django_migrations表中相关的同步记录删除;
. 再次同步 python3 manage.py makemigrations; python3 manage.py migrate
5 'QuerySet' object has no attribute 'book_set'
解决方法:
QuerySet没有_set方法,必须是Object才可以,所以使用first()将对象从列表中取出
示例:
obj = Publisher.objects.filter(name="机械出版社")first()
print(obj.book_set.all().values("title"))
6 django.db.utils.OperationalError: no such table: django_session
解决方法:
这是因为还没有进行数据库同步,数据库中还没有创建相关联的表
python3 manage.py makemigrations
python3 manage.py migrate
7 RuntimeWarning: DateTimeField received a naive datetime
报错原因:
这是由于Django默认使用的时间都是带时区的,而我们在插入数据时使用的datetime是不带时区的。
解决方法:
方案一:
把setting.py文件中USE_TZ改为false,默认不使用时区
方案二:
pip3 install pytz
import pytz
datetime.now(tz=pytz.UTC)
导入pytz模块,datetime生成时间时带上时区
8 Got an error creating the test database: (1007, "Can't create database 'test_django_db'; database exists")
Type 'yes' if you would like to try deleting the test database 'test_django_db', or 'no' to cancel:
原因:
Django test数据库自动生成的数据库字符集是latin1,不是utf8,所以当表中有中文会报错 解决方法:
https://docs.djangoproject.com/en/1.11/ref/settings/#charset
设置test数据库字符集为“utf8”
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': 'mysql',
'HOST': '',
'PORT': '',
'TEST': {'CHARSET': 'utf8', },
}
}
9 UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list:
原因:
因为obj.objects.all()的结果是无序的,Paginator分页会出错 解决方法:
在查询结果上加上order_by排序, obj.objects.all().order_by("id")
其它:
setting.py文件中设置的时区是"Asia/Shanghai",数据库中存储的时间是UTC时间,模板中按照资料说应该自动渲染成东八区,
但是没有,要么手动给他增加8个小时,要么通过astimezone方法更改它的时区 解决方法(column_data是带时区的时间):
import pytz
from django.utils.timezone import datetime, timedelta
tz = pytz.timezone("Asia/Shanghai")
# 笨方法:
column_data = (column_data + timedelta(hours=)).replace(tzinfo=tz).strftime("%Y-%m-%d %H:%M:%S %Z") # 聪明方法:
column_data = column_data.astimezone(tz=tz).strftime("%Y-%m-%d %H:%M:%S %Z")
# 如果手动修改了数据库中的记录,再migration同步时会出错 解决方法:
. python3 manage.py makemigrations
. python3 manage.py migrate --fake
最新文章
- Linux shell之打印输出
- 使用NuGet打包并发布至ProGet过程 (步骤详细,附python脚本)【上篇】
- .NET Framework 中的所有类型
- jvm基础笔记
- The Kernel Newbie Corner: Kernel Debugging with proc ";Sequence"; Files--Part 3
- ExtJS 自定义组件
- Nagiosserver端安装部署具体解释(1)
- 《JavaScript DOM编程艺术》读书笔记
- Git一些其它的功能
- 用mapreduce 处理气象数据集
- python多任务-线程
- Linux拉你入门
- 矩阵的SVD分解
- jquery实现元素高度变化页面变化
- 从零开始学习html(十二)CSS布局模型——下
- 华Xia相机WEB后台设置
- Round() 四舍五入 js银行家算法
- 用liferay实现的增删改查例子-book管理系统
- 小程序四:视图之WXSS
- Oracle某些功能实现语句处理方法
热门文章
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
- sersync配置
- Opencv中的轮廓(不全)
- ruoyi ShiroUtils
- 吴裕雄--天生自然深度学习TensorBoard可视化:projector_data_prepare
- Python笔记_第一篇_面向过程_第一部分_9.Ubuntu基础操作
- 一、Cookie和Session介绍
- 扯下Python的super()
- Debian8.8同步时间
- $.post系统登录校验