初学python的Djongo的问题汇总(从某宝买的某马Python教学视频,只有视频,没有软件)

一、File "/home/jayce/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query

query = query.decode(errors='replace')

AttributeError: 'str' object has no attribute 'decode'

错误代码:query = query.decode(errors='replace')

解决方法:把decode改为encode即可 query = query.encode(errors='replace')

二、  File "/home/jayce/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 36, in <module>

    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

解决方法:找到这个文件:/anaconda3/lib/python3.7/site-packages/django/db/backends/mysql/base.py

version = Database.version_info
if version < (1, 3, 3):
    raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

中的下面注释掉:
   # raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

三、Django:报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

Django 执行迁移生成表

python manage.py migrate

报错:

raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)

原因:Django2.1不再支持MySQL5.5,必须5.6版本以上

解决办法:原链接:https://www.cnblogs.com/yebaofang/p/9863678.html

(1)Django降级到2.0

先卸载掉当前的Django :pip uninstall Django

在安装:pip install Django==2.0.0

(2)MySQL升级

四、执行迁移生成表 python manage.py migrate时报错:

  File "/home/jayce/jayce/test2/booktest/models.py", line 20, in <module>
    class HeroInfo(models.Model):
  File "/home/jayce/jayce/test2/booktest/models.py", line 28, in HeroInfo
    hbook = models.ForeignKey('BookInfo')
TypeError: __init__() missing 1 required positional argument: 'on_delete'

解决方法:找到,并按提示添加一个位置参数on_delete=models.CASCADE即可,如:hbook = models.ForeignKey('BookInfo',on_delete=models.CASCADE,)

总结,安装软件难免出错,万事找百度,只要有信心,就可以找到答案。

附上一些今日学习的Djongo小学一点知识:

一、创建项目和应用

1.django-admin startproject test1 项目的创建
进入 test1目录中
2.python manage.py startapp booktest 创建应用

3.建立应用与项目的联系,需要对应用进行注册
修改项目目录下settings.py中的INSTALLED_APPS 在最后加上'booktest',#进行应用的注册

4.运行web服务器命令
python manage.py runserver

二、关于数据库连接mysql:

在项目目录下的setting.py中添加自己数据库的配置

DATABASES = {
    'default': {
        #'ENGINE': 'django.db.backends.sqlite3',
        'ENGINE': 'django.db.backends.mysql',#数据库手动创建
        #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'NAME': 'bj18',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': 'xxxx.xxxx.xx.xxx',
        'PORT':'43306',
    }
}

在setting.py还可以修改下面内容,进行本地化

LANGUAGE_CODE = 'zh-hans' # 使用中文

TIME_ZONE = 'Asia/Shanghai' # 使用上海时间

三、在models.py中:创建一些建表的信息的

1.生成迁移文件
python manage.py makemigrations 
2.执行迁移生成表
python manage.py migrate

猜你喜欢

转载自blog.csdn.net/qq_25162431/article/details/102470789