Django错误大汇总

2023-07-31,,

1.安装django报错解决方案  

找到第一条报错信息:
  File "c:\users\chenwei\envs\testvir2\lib\site-packages\pip\basecommand.py", line 215, in main status = self.run(options, args)    
  出错原因:UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' in position 8: ordinal not in range(128)
  在报错文件:c:\users\chenwei\envs\testvir2\lib\site-packages\pip\basecommand.py 中加如下代码:

import sys
reload(sys)
sys.setdefaultencoding('gbk')

2.错误2:RuntimeError: maximum recursion depth exceeded

解决方案:
To fix the problem replace this (about line 56 in python\Lib\fuctools.py):
convert = {
'__lt__': [('__gt__', lambda self, other: other < self),
('__le__', lambda self, other: not other < self),
('__ge__', lambda self, other: not self < other)],
'__le__': [('__ge__', lambda self, other: other <= self),
('__lt__', lambda self, other: not other <= self),
('__gt__', lambda self, other: not self <= other)],
'__gt__': [('__lt__', lambda self, other: other > self),
('__ge__', lambda self, other: not other > self),
('__le__', lambda self, other: not self > other)],
'__ge__': [('__le__', lambda self, other: other >= self),
('__gt__', lambda self, other: not other >= self),
('__lt__', lambda self, other: not self >= other)]
}
to that:
convert = {
'__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
('__le__', lambda self, other: self < other or self == other),
('__ge__', lambda self, other: not self < other)],
'__le__': [('__ge__', lambda self, other: not self <= other or self == other),
('__lt__', lambda self, other: self <= other and not self == other),
('__gt__', lambda self, other: not self <= other)],
'__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
('__ge__', lambda self, other: self > other or self == other),
('__le__', lambda self, other: not self > other)],
'__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
('__gt__', lambda self, other: self >= other and not self == other),
('__lt__', lambda self, other: not self >= other)]
}

3.在python中run manage.py 链接数据库报错:No name mysqlDB

解决方案:pip install mysql-python

4.安装mysql-python报错

错误提示:
    error: command '"C:\Users\chenwei\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe"' failed with exit status2

解决方案:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python下载对应的包版本 pip2 install MySQL_python-1.2.5-cp27-none-win_amd64.whl
注意:cp27说明要用python2.7来安装,所以要用pip2

5.执行python2 manage.py runserver 0.0.0.0:8000出错

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 62, in exe
super(Command, self).execute(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 330, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 101, in ha
self.run(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.py", line 110, in ru
autoreload.main(self.inner_run, None, options)
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 341, in main
reloader(wrapped_main_func, args, kwargs)
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 312, in python_reloader
exit_code = restart_with_reloader()
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 294, in restart_with_reloade
str_value = force_bytes(new_environ[key], encoding=encoding)
File "C:\Python27\lib\site-packages\django\utils\encoding.py", line 124, in force_bytes
return s.decode('utf-8', errors).encode(encoding, errors)
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd4 in position 15: invalid continuation byte

解决方案:

在manage.py 开头加上
import sys
reload(sys)
sys.setdefaultencoding('utf-8')

6.当创建的app比较多时,将多个app存放在一个文件夹apps下,提示找不到文件路径

  python manage.py runserver 0.0.0.0:8000

  解决方案:

在settins文件中,添加sys.path.insert(0, os.path.join(BASE_DIR, 'apps')) 

7.Django “Cannot add or update a child row: a foreign key constraint fails”

  解决方案:

settins下
DATABASES = {
'default': {
...
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
},
}
}

8.通过源码安装xadmin,执行manage.py时,报找不到xadmin模块

解决方案:将xadmin添加到搜索路径下。

9.django.core.exceptions.ImproperlyConfigured: Application names aren't unique, duplicates: my_app01

 解决方案一:

    在settings中,删除掉你注册的应用:

 解决方案二:

   找到你所写的应用app01---->把apps.py文件中的代码全部注释掉。即可解决问题 

10.'WSGIRequest' object has no attribute 'user'

  将settings中配置的MIDDLEWARE改为MIDDLEWARE_CLASSES

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

  

Django错误大汇总的相关教程结束。

《Django错误大汇总.doc》

下载本文的Word格式文档,以方便收藏与打印。