Django搭建中遇到的错误总结

错误1

python3 与 Django 连接数据库:Error loading MySQLdb module: No module named ‘MySQLdb’

解决方法:在 init.py 文件中添加以下代码即可。

import pymysql

pymysql.install_as_MySQLdb()

错误2

在执行python manage.py migrate报一下错误

django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near ‘(6) NOT NULL)’ at line 1”))

请检测你的环境版本,Django2.1只支持mysql5.6以上的版本!只需要升级你的mysql版本或降django到2.0即可。

错误3

解决Django admin 插入中文时候出现乱码问题

ALTER TABLE blog_blogpost MODIFY COLUMN body VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL;
// 需要注意的是 blog_blogpost 这个表示的是 表名 body 这个是列名 要根据自己项目所需要的 表和列进行修改

错误4

Django分页出现UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered ob

messeges = MessegeModel.objects.all()

变为

messeges = MessegeModel.objects.get_queryset().order_by('id')

错误5

RuntimeWarning: DateTimeField Message.publish received a naive datetime (2018-12-12 16:29:58) while time zone support is active.
RuntimeWarning)

settings.py中的USE_TZ= True的原因,把True改成False即可。

错误6

想测试数据直接运行单独的py文件会报以下错误。

Traceback (most recent call last):
File “G:/medlar/operation/DaluCloud2/publish/test.py”, line 6, in
from publish.models import Message
File “G:\medlar\operation\DaluCloud2\publish\models.py”, line 4, in
class Message(models.Model):
File “C:\Python36\lib\site-packages\django\db\models\base.py”, line 100, in new
app_config = apps.get_containing_app_config(module)
File “C:\Python36\lib\site-packages\django\apps\registry.py”, line 244, in get_containing_app_config
self.check_apps_ready()
File “C:\Python36\lib\site-packages\django\apps\registry.py”, line 127, in check_apps_ready
raise AppRegistryNotReady(“Apps aren’t loaded yet.”)
django.core.exceptions.AppRegistryNotReady: Apps aren’t loaded yet.

此时需要在想单独运行py文件中如以下代码

#加入的代码
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DaluCloud2.settings")  #DaluCloud2为自己工程的名字
django.setup()
######################要加载最上面
from publish.models import Message

messages = Message.objects.values("id").all()
print(messages)

错误7

/usr/local/python3/bin/python3 manage.py migrate

System check identified some issues:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection ‘default’
HINT: MySQL’s Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.1/ref/databases/#mysql-sql-mode

解决办法:settings.py文件夹加入DATABASES[‘OPTIONS’][‘init_command’] = “SET sql_mode=‘STRICT_TRANS_TABLES’”

DATABASES = {
      'default': {
          'ENGINE': 'django.db.backends.mysql',
          'NAME': 'stock',
          'USER': 'root',
          'PASSWORD': 'root',
          'HOST':'127.0.0.1',
          'PORT':'3306',
          'OPTIONS': {
              "init_command": "SET sql_mode='STRICT_TRANS_TABLES'",                                                              
          }
      }
  }

方法2:
修改myql的配置文件/etc/my.cnf,重启后对所有连接生效
在[mysqld]下面添加如下列:

sql_mode=STRICT_TRANS_TABLES
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值