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 分钟前
直播切片素材杂乱不好复用,易元AI要怎么处理
大数据·人工智能·python
怪奇云呼军40 分钟前
从声音特征到 CRM 回流:闪电智能 Voice Agent 沟通策略自适应系统 v1 实战
android·人工智能·python·音视频·语音识别
jufeng13071 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 6 篇】
python·ai agent·记忆系统
kevinnett2 小时前
别再把模型地址写死了:用 Python 设计一个可切换的 LLM 调用层
python
天天进步20152 小时前
Python全栈项目--协同办公平台
开发语言·python
GEOshijie1233 小时前
GEO服务商算法适配承诺怎么验收?48小时响应的合同化考核方案
人工智能·python
卷无止境3 小时前
FastAPI 前端托管全攻略:从静态文件到大型全栈项目架构
后端·python
QQ5416451213 小时前
【驿帮查件助手python开源】开源聚合查件机器人,实现 24 类驿站批量查件取代短信通知。技术方案分享
python·机器人·开源·驿站查件机器人
卷无止境3 小时前
当FastAPI项目开始"膨胀",代码该往哪儿放
后端·python
用户8356290780514 小时前
Python设置PowerPoint幻灯片背景的方法
后端·python