Django那些坑坑!

Django Admin开发时css样式丢失

百度了很多种解决方法,如settings.py中添加STATIC_ROOT的python路径,或者自己创建static文件夹然后执行python manage.py collectstatic等等。

都不好用,都不好用,都不好用!!!!

原因:admin/base.html文件出问题

解决方案:base.html文件路径C:\Python27\Lib\site-packages\django\contrib\admin\templates\admin

      1. 删除base.html头不中的 <!DOCTYPE html>
      2. 添加<meta http-equiv='Content-type' content='text/htm'>(不是必填项,但建议添加)

详细参照:https://www.cnblogs.com/shizhengwen/p/6622071.html

Django默认数据库更换到mysql数据库后,guest add报错

原因:不知道

解决方案:

    1. guest目录下Python manage.py makemigrations
    2. guest目录下python manage.py migrate

注意:更换数据库后,原来数据库上的设定账号都没有了。需要从新分配账户(python manage.py createsuperuser)

Django连接数据库查询后报Warning(3135,3090)

原因:数据库settings.py中数据库模式设定成了"STRICT_TRANS_TABLES".

解决方案:修改settings.py中的database设定,SET sql_mode='STRICT_TRANS_TABLES' > SET sql_mode='traditional'

models中数据类型后期更正时刷新页面会报error

原因:虽然将既存数据全部闪光,但仍然无法解决。刷新页面依然报错,必须保证model中的数据类型和数据库中的数量相同并一致

解决方案:通过mysql试图工具(navicat for mysql)通过设计表直接更改或添加相应的项目即可。

关于RuntimeWarning的解决办法

报错:

C:\Python27\lib\site-packages\django\db\models\fields\__init__.py:1451: RuntimeW
arning: DateTimeField Event.start_time received a naive datetime (2016-08-31 05:
18:00) while time zone support is active.
RuntimeWarning)

原因:以后遇到时区问题时,还需要进一步调查解决。

解决方法:settings.py文件中注释掉USE_TZ = True(暂时的对策案,没有从根本上解决)

用django测试时报错解决过程

最开始报错:django.db.utils.InternalError: (1054, u'Unknown error 1054')

Got an error creating the test database: (1007, u'Unknown error 1007')
Type 'yes' if you would like to try deleting the test database 'test_guest', or
'no' to cancel: yes

--------------如上选yes后,仍然报错,解决方案移除了migrations文件夹下的所有内容,又重新做了一遍数据迁移---------------------

  1. Python manage.py makemigrations
  2. guest目录下python manage.py migrate

--------------------然后然后又报了介个错误----------------

django.db.utils.IntegrityError: 1452

原因:现在的我还悲哀的不知道为什么涅5555

解决方案:在settings.py databases设定出追加一行

'OPTIONS':{
"init_command": "SET foreign_key_checks = 0;",

测试中报错UnicodeEncodeError: 'cp932

看似是一个文字解码错误,实际上是因为脚本中的装饰器所依赖的参数没有提供

解决方案:将脚本中对应内容的views.py文件中的装饰器@login_required注释掉

还有一次是因为脚本中创建的内容与数据库中的字段不一致,如Guest表中用event_id则会报本错误,更正为event_id_id则此问题可消除

当然了,还有一次确实是因为文字解码,把测试case中的注释删掉就恢复了(可能是因为注释''''''输入时的输入法不正确)

self.assertIn(b"xxxx")中的b

python 2.x版本中如果self.assertIn(“xxxx”)中string前没有添加b则会报如下错误

UnicodeEncodeError: 'cp932' codec can't encode character u'\u6237' in position 79: illegal multibyte sequence,

这与from __future__ import unicode_literals导入语句有关,如果注释掉它,self.assertIn("xxxx")中的string前则不需要添加b了。

https://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal

"Actually, if you've imported unicode_literals from __future__, this will "reverse" the behavior for this particular string (in Python 2.x)"

shell中的the test views

现象:如果是在python manage.py shell中需要先行初始化测试环境,否则会报错

步骤:from django.test.utils import setup_test_environment

         setup_test_environment()

简单的测试示例
>>>from django.test.utils import setup_test_environment
>>>setup_test_environment()#初始化测试环境
>>>from django.test import Client #导入Client()测试类
>>>c = Client()
>>>response = c.get('/index/'#用client()类提供的get()方法测试index页面
>>>response.status_code  #得到返回码200表示成功
200
>>>response.content  #得到页面内容
'<html>\n  <head>\n    <title>Django Page</title>\n  </head>\n\n  <body>\n    <h
1>\xe5\x8f\x91\xe5\xb8\x83\xe4\xbc\x9a\xe7\xae\xa1\xe7\x90\x86\xe7\xb3\xbb\xe7\x
bb\x9f</h1>\n    <form method="post" action="/login_action/">\n      <input name
="username" type="text" placeholder="username" ><br>\n      <input name="passwor
d" type="password" placeholder="password" ><br>\n      <br>\n      <button id=\'
btn\' type=\'submit\'>\xe7\x99\xbb\xe5\xbd\x95</button>\n      <input type=\'hid
den\' name=\'csrfmiddlewaretoken\' value=\'3V6TQyr15vMog87kaYXzFivFPMfcnyYHlXLvr
vBltrWHO5jz06Bxk6z76vmTFGPX\' />\n    </form>\n  </body>\n</html>'

调用post方法的接口时一直报500(此坑里面趴三天!)

报错详细:ValueError: invalid literal for int() with base 10:' ' ....500

原因:postman调用时设定的是json形式,

分析思路:此报错表示不能把空字符串转成int,即从request取数据时有问题。content-type=json请求时,不会把传过来的数据放到request.POST里

               所以request.POST里是空的

对应策略:

json.loads(json_str):将接受的json字符串转换成python结构数据

body = json.loads(request.body)。 可以从request.body中获取请求体中的数据,如body['eid']获取eid的数据

cmd中执行python views_if.py,models.py等文件时报错(此坑和上坑一起趴三天!)

现象:python views_if.py时报如下错误

cmd里run py文件
Traceback (most recent call last):
  File "views_if.py", line 11, in <module>
    from sign.models import Event, Guest
  File "F:\Auto_Projects\pydj\guest\sign\models.py", line 8, in <module>
    class Event(models.Model):
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 110, in __
new__
    app_config = apps.get_containing_app_config(module)
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 247, in get
_containing_app_config
    self.check_apps_ready()
  File "C:\Python27\lib\site-packages\django\apps\registry.py", line 125, in che
ck_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

原因:错误原因是不是程序入口,引用的包还没有初始化。views_if.py文件中用到了django的内容,需要用django的方式来启动。

解决办法:不要用cmd的方式来run还有django内容的py文件

posted @ 2019-04-11 09:02  caohaizhusha  阅读(468)  评论(0)    收藏  举报