【Django】【问题集锦】
参考:http://www.linuxidc.com/Linux/2013-03/80649.htm
1. Django的shell模式下,如果报warning,则再执行一次,也许就好了
2. Django的shell模式下,如果报错说某个变量没有某个属性,再定义次这个变量,也许就好了,因为可能是变量过期了?
3. django中的一个数据库错误 django.db.utils.InternalError: 1366 : mysql数据编码的问题,我的解决方式就简单粗暴了(因为项目是初创,所以不担心数据备份等问题)
(1)数据库客户端删除数据库
(2)清理项目中关于数据库的缓存 :删掉migrations和 __pycache__ folder
(3)数据库客户端创建数据库,编码格式选择"utf-8"
(4)重新执行:
python3 manage.py makemigrations app名字
python3 manage.py migrate
4. pymysql.err.InternalError: (1364, "Field 'create_time' doesn't have a default value") :时间应该是默认当前时间,是models创建时间字段的设置的,具体怎么设置怎么也没搞明白。。。大家有高招请赐教~~
5. guest 项目,登录页输入信息点击“登录”要跳转到login_action时候,报错
RuntimeError at /login_action
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to localhost:8000/login_action/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings
【解决步骤1】我按照提示
set APPEND_SLASH=False in your Django settings 做了,但是报另外一个错误
Page not found (404)
【解决步骤2】不
set APPEND_SLASH=False in your Django settings 了,我发现是因为在index.html中的action 应该是 '/login_action/',而不是'/login_action'
6.
当第一次用session时候,报错“ “no such table: django_session”,执行下方操作
D:\pydj\guest>python3 manage.py migrate
7. page not found 防止的办法们:
不能随便删除原生文件,再创建个相同名字的;
重新在pycharm中打开项目时,要选择用Python版本;
8. 首次于 window7 cmd.exe 运行sign 这个APP的testcase时候,遇到“python testcase got an error creating the test database:(1007)” ,此时选择yes 即可
9. django "RuntimeWarning:DateTimeField receive a naive datetime while time zone support is active"
参考:http://blog.csdn.net/laughing2333/article/details/53513414
我暂时没时间研究
10. Django invalid http header :'testserver' .you may need to add 'testserver' to allowed_hosts
参考:http://blog.csdn.net/will5451/article/details/53861092
11. shell 状态,
>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> from django.test import Client
>>> c = Client()
>>> response = c.get('/index/')
运行 测试用例,注意上面的问题。如果python manage.py test sign.* 这种形式的,需要把
ALLOWED_HOSTS = [] ,否则会找不到页面,具体原因不明
12. 首次编辑models.py 然后浏览器访问 http://127.0.0.1:8000/admin 后台日志报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
解决办法:
将报错行的代码改为:
event = models.ForeignKey(Event, on_delete=models.CASCADE) # 关联发布会ID
即在外键值的后面加上 on_delete=models.CASCADE
13
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.2
C:\Python37\Lib\site-packages\django\db\backends\mysql(python安装目录)打开base.py,注释掉以下内容:
if version < (1, 3, 13): raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.’ % Database.version)
14
File “C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query
query = query.decode(errors=‘replace’)
AttributeError: ‘str’ object has no attribute ‘decode’
解决办法:
打开此文件把146行的decode修改为encode