Skip to main content

5.Django 报错汇总

报错:在windows上运行django时报错:python Error: You don't have permission to access that port.

解决方案: Try changing the ports in your settings.py instead of default 8080 port

from django.core.management.commands.runserver import Command as rs
rs.default_port='5000'

参考: Windows Django: Error: You don't have permission to access that port - Stack Overflow

报错:为什么我安装了django后 目录里面没有bin文件

解决方案: django 3.0以下是有bin文件的,4.0后就移除了bin文件夹但是还是可以正常使用没问题的

使用带管理员权限的powershell重新安装django

pip3 install Django=4.2.3

参考: 为什么我安装了django后 目录里面没有bin文件-有问必答-CSDN问答

报错:添加rest_framework模块是报错:Django Rest Framework -- no module named rest_framework

解决方案:

pip3 install djangorestframework

参考: https://stackoverflow.com/questions/33308781/django-rest-framework-no-module-named-rest-framework

报错:django.db.utils.OperationalError: no such table: auth_user

**背景:**django-admin startproject xxx 开始一个新项目后无法使用manage的功能,报错如上

解决方案: 新建一个项目后需要迁移数据库,按照django提示操作,提示如下:

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): 
admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

所以运行以下命令即可:

python manage.py migrate

报错:在Django中引入redis时,报类似错:没有 get_redis_connection方法

解决方案:导入以下包

from django_redis import get_redis_connection

参考: python Django redis 连接配置_qq_36606793的博客-CSDN博客

报错:Django中引入mysql时报错:"cryptography is required for sha256_password or caching_sha2_password"

解决方案: pip安装加密包

pip install cryptography

参考: https://stackoverflow.com/questions/54477829/cryptography-is-required-for-sha256-password-or-caching-sha2-password

报错:调试Django时,只能用127.0.0.1:8000端口方案,用django所在的服务器IP:8000无法访问

解决方案: 带以下参数启动Djano

python3 manage.py runserver 0.0.0.0:8000

操作后可能还会报“allow_host”的问题,按提示把IP加入setting.py中的all_host就行

报错:django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).

**原因:**pip安装的Django版本太低了:查询版本命令python3 -m django --version

**解决方案:**使用源码重新安装Django版本

git clone https://github.com/django/django.git   //大小大概240M
pip3 install -e django/ #从本地安装Django

报错:django.core.exceptions.ImproperlyConfigured: Cannot import 'users'. Check that 'apps.users.apps.UsersConfig.name' is correct.

**解决方案:**AppConfig.name should be the full Python path to the application. Since you appear to have users inside an apps module, you should use 'apps.users' instead of 'users'

class UsersConfig(AppConfig):
name = 'apps.users'

参考:https://stackoverflow.com/questions/67056517/django-3-2-exception-django-core-exceptions-improperlyconfigured

报错:ValueError: Invalid model reference 'app.users.User'. String model references must be of the form 'app_label.ModelName'.

**解决方案:**The error was that I've specified models in the path to the MyUser model:

AUTH_USER_MODEL = 'users.models.MyUser'

But we shouldn't do it, we just need specify the package and model name only

AUTH_USER_MODEL = 'users.MyUser'

参考:https://stackoverflow.com/questions/47392670/must-be-of-the-form-app-label-modelname-model-valueerror-invalid-model-re