以下这行代码
from django.shortcuts import render_to_response
会报以下错误
ImportError: cannot import name 'render_to_response' from 'django.shortcuts'
原因是在Django 2已经弃用了render_to_response, 应当替换成以下代码
from django.shortcuts import render
执行 python manage.py migrate 时出现错误:
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'localhost' (10061)")
仔细阅读发现需要安装本地 mysql,然后去官网下载,按照 mysql 教程安装,现在已经是 8.0.17 版本了,教程的安装设置步骤有所变化,搜索了了好多,发现这个可用:解决mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT。设置成功后别忘了新建一个库,之后在执行那个命令就可以了。
非多站模式时 vhost = true 和 no-site = true 需要注释掉,否则后续 nginx 配置文件中设置的入口文件则不生效,服务器会回应 Internal Server error:
[uwsgi] socket = 127.0.0.1:9090 master = true //主进程 #vhost = true //多站模式 #no-site = true //多站模式时不设置入口模块和文件 workers = 2 //子进程数 reload-mercy = 10 vacuum = true //退出、重启时清理文件 max-requests = 1000 limit-as = 512 buffer-size = 30000 pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程 daemonize = /website/uwsgi9090.log
安装 uwsgi 如果失败,有可能是缺少Python的头文件和静态库,需要安装开发版本:
For apt (Ubuntu, Debian...):
sudo apt-get install python-dev # for python2.x installs sudo apt-get install python3-dev # for python3.x installs
For yum (CentOS, RHEL...):
sudo yum install python-devel
For dnf (Fedora...):
sudo dnf install python2-devel # for python2.x installs sudo dnf install python3-devel # for python3.x installs
For zypper (openSUSE...):
sudo zypper in python-devel # for python2.x installs sudo zypper in python3-devel # for python3.x installs
Django 在根据 models 生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete' 错误
原因:
在 django2.0 后,定义外键和一对一关系的时候需要加 on_delete 选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing 1 required positional argument: 'on_delete'。
举例说明:
user=models.OneToOneField(User)owner=models.ForeignKey(UserProfile)
需要改成:
user=models.OneToOneField(User,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值 owner=models.ForeignKey(UserProfile,on_delete=models.CASCADE) --在老版本这个参数(models.CASCADE)是默认值参数
说明:on_delete 有 CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET() 五个可选择的值。
感谢您的支持,我会继续努力的!
支付宝扫一扫,即可进行扫码打赏哦
2628Django 表单
以下这行代码
会报以下错误
ImportError: cannot import name 'render_to_response' from 'django.shortcuts'
原因是在Django 2已经弃用了render_to_response, 应当替换成以下代码
2627Django 模型
执行 python manage.py migrate 时出现错误:
仔细阅读发现需要安装本地 mysql,然后去官网下载,按照 mysql 教程安装,现在已经是 8.0.17 版本了,教程的安装设置步骤有所变化,搜索了了好多,发现这个可用:解决mysql8报错:ERROR 1410 (42000): You are not allowed to create a user with GRANT。设置成功后别忘了新建一个库,之后在执行那个命令就可以了。
1243Django Nginx+uwsgi 安装配置
非多站模式时 vhost = true 和 no-site = true 需要注释掉,否则后续 nginx 配置文件中设置的入口文件则不生效,服务器会回应 Internal Server error:
1242Django Nginx+uwsgi 安装配置
安装 uwsgi 如果失败,有可能是缺少Python的头文件和静态库,需要安装开发版本:
For apt (Ubuntu, Debian...):
For yum (CentOS, RHEL...):
For dnf (Fedora...):
For zypper (openSUSE...):
1241Django Admin 管理工具
Django 在根据 models 生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete' 错误
原因:
在 django2.0 后,定义外键和一对一关系的时候需要加 on_delete 选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing 1 required positional argument: 'on_delete'。
举例说明:
需要改成:
说明:on_delete 有 CASCADE、PROTECT、SET_NULL、SET_DEFAULT、SET() 五个可选择的值。