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 新密码 修改密码,注意结尾分号
  • 使用新的密码连接数据库即可
相关推荐
Amo Xiang41 分钟前
Python 解释器安装全攻略(适用于 Linux / Windows / macOS)
linux·windows·python·环境安装
程序员杰哥43 分钟前
接口自动化测试之pytest 运行方式及前置后置封装
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
浩皓素1 小时前
用Python开启游戏开发之旅
python
hello kitty w2 小时前
Python学习(6) ----- Python2和Python3的区别
开发语言·python·学习
互联网杂货铺3 小时前
功能测试、性能测试、安全测试详解
自动化测试·软件测试·python·功能测试·测试工具·性能测试·安全性测试
土豆杨6263 小时前
隐藏层-机器学习
python·机器学习
Dxy12393102164 小时前
DrissionPage调试工具:网页自动化与数据采集的革新利器
爬虫·python·drissionpage
不争先.5 小时前
URL 结构说明+路由(接口)的认识
python·pycharm·flask·apifox
(・Д・)ノ5 小时前
python打卡day44
人工智能·python·机器学习
胡西风_foxww5 小时前
Python 入门到进阶全指南:从语言特性到实战项目
开发语言·python·快速入门