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 新密码 修改密码,注意结尾分号
  • 使用新的密码连接数据库即可
相关推荐
谙弆悕博士8 分钟前
Python快速学习——第8章:循环语句
python·学习·servlet
idingzhi12 分钟前
A股量化策略日报(2026年05月09日)
python
Mr数据杨16 分钟前
【Codex】用知识点配置模块构建考试与教学知识图谱
人工智能·django·知识图谱·codex·项目开发
m0_6245785918 分钟前
C#怎么获取U盘的插拔事件_C#如何重写WndProc捕获消息【进阶】
jvm·数据库·python
我叫黑大帅26 分钟前
PyScript-GitHubRepo: 构建高性能GitHub仓库批量下载工具的技术实践
后端·python·面试
lbb 小魔仙39 分钟前
基于Python构建RAG(检索增强生成)系统:从原理到企业级实战
开发语言·python
SunnyDays10111 小时前
Python 如何精准统计 Word 文档的页数、字数、行数
python·word文档字数统计
小陈的进阶之路1 小时前
Python系列课(2)——判断
java·前端·python
脉动数据行情2 小时前
Python 实现融通金行情数据对接(实时推送 + K 线 + 产品列表)
开发语言·python
wltx16882 小时前
谷歌SEO如何做插床优化?
大数据·人工智能·python