Django框架FAQ

文章目录

    • 问题1:Django数据库恢复
    • 问题2:null和blank的区别
    • 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
    • 5.报错 RuntimeError: cryptography is required for sha256_password or caching_sha2_password

问题1:Django数据库恢复

问题:

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

解决方法:

# 步骤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‘“)

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'

Django框架FAQ_第1张图片

解决方法:

#只需要在 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命令无效

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 新密码 修改密码,注意结尾分号
  • 使用新的密码连接数据库即可

你可能感兴趣的:(2.Web框架-django,django,python)