Django框架FAQ

文章目录

    • 问题1:Django数据库恢复
    • 问题2:null和blank的区别
    • [3.报错 django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")](#3.报错 django.db.utils.IntegrityError: (1062, “Duplicate entry ‘‘ for key ‘mobile‘“))
    • [4.报错 Refused to display 'url' in a frame because it set 'X-Frame-Options' to 'deny](#4.报错 Refused to display ‘url‘ in a frame because it set ‘X-Frame-Options‘ to 'deny)
    • [5.报错 RuntimeError: cryptography is required for sha256_password or caching_sha2_password](#5.报错 RuntimeError: cryptography is required for sha256_password or caching_sha2_password)

问题1:Django数据库恢复

问题:

从仓库拉下来的Django项目,没有sqlite数据库和migrations记录,如何通过model恢复数据库

解决方法:

python 复制代码
# 步骤1:导出数据
# 不指定 appname 时默认为导出所有的app
python manage.py dumpdata [appname] > appname_data.json

#步骤2导入数据
python manage.py loaddata blog_dump.json

问题2:null和blank的区别

问题:

Django models中的null和blank的区别

解决方法:

null blank
null是在数据库上表现NULL blank只是在填写表单的时候可以为空,而在数据库上存储的是一个空字符串

注意:

日期型(DateField,TimeField,DateTimeField)和数字型(IntegerField,DecimalField,FloatField)不能接受空字符串,如要想要在填写表单的时候这两种类型的字段为空的话,则需要同时设置null=True,blank=True

3.报错 django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")

问题:

报错:django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")

python 复制代码
The above exception was the direct cause of the following exception:
...
File "C:\Users\Jason\AppData\Roaming\Python\Python36\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\Users\Jason\AppData\Roaming\Python\Python36\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
django.db.utils.IntegrityError: (1062, "Duplicate entry '' for key 'mobile'")

解决方法:

众所周知我们创建后台管理员时候是没有设置手机号的.报这个错就是告诉我们在数据库中已经存在一个管理员,且手机号为空,我们再次创建管理员就会有手机号重复的现象存在从而报这个错. username重复时同样出现以下问题

4.报错 Refused to display 'url' in a frame because it set 'X-Frame-Options' to 'deny

问题:

运行Django时浏览器中遇到Refused to display 'url' in a frame because it set 'X-Frame-Options' to 'deny'

解决方法:

python 复制代码
#只需要在 Djagno 的 settings.py 文件中输入:
X_FRAME_OPTIONS = 'SAMEORIGIN'

5.报错 RuntimeError: cryptography is required for sha256_password or caching_sha2_password

现象1:

使用Django2.0连接数据库的时候报错RuntimeError: cryptography is required for sha256_password or caching_sha2_password

现象2:

使用数据库连接工具连接时报错:mysql:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO/YES)

本地环境: windows10+mysql8.0.21+django2.0.1

解决方案:

  • 以管理员身份打开cmd 输入 net stop mysql
  • 接着输入 mysqld --console --skip-grant-tables --shared-memory
  • 在重新打开一个cmd 依次输入 mysql -u root -p 提示输入password再按Enter

注意:如果没有设置环境变量则会显示mysql命令无效

sql 复制代码
show databases
use mysql
update user set authentication_string = '' where user='root';
  • 以管理员身份新打开cmd 输入 net start mysql
  • 新cmd中mysql -uroot -p , 按Enter, 出现"Enter password:" , 再按Enter
  • ALTER USER 'root'@'localhost' IDENTIFIED BY 新密码 修改密码,注意结尾分号
  • 使用新的密码连接数据库即可
相关推荐
路人蛃1 小时前
Scikit-learn - 机器学习库初步了解
人工智能·python·深度学习·机器学习·scikit-learn·交友
Nep&Preception3 小时前
vasp计算弹性常数
开发语言·python
费弗里4 小时前
Python全栈应用开发神器fac 0.4.0新版本升级指南&更新日志
python·dash
Ice__Cai4 小时前
Python 基础详解:数据类型(Data Types)—— 程序的“数据基石”
开发语言·后端·python·数据类型
lilv664 小时前
python中用xlrd、xlwt读取和写入Excel中的日期值
开发语言·python·excel
程序员果子5 小时前
macOS Python 安装
python·macos
LetsonH5 小时前
⭐CVPR2025 RoboBrain:机器人操作的统一大脑模型[特殊字符]
人工智能·python·深度学习·计算机视觉·机器人
站大爷IP6 小时前
Django缓存机制详解:从配置到实战应用
python
叫我:松哥6 小时前
基于Python的实习僧招聘数据采集与可视化分析,使用matplotlib进行可视化
开发语言·数据库·python·课程设计·matplotlib·文本挖掘
站大爷IP6 小时前
Django模型开发全解析:字段、元数据与继承的实战指南
python