django: 开发服务器下admin界面没有css

来源:互联网 发布:知乎怎么更新答案 编辑:程序博客网 时间:2024/06/06 05:36

为了省心,选择了bitnami的集合软件: bitnami-djangostack-1.5.5-1-windows-installer。

安装完成后,python, django, apache, postgresql, sqlite3全都有了,python版本是2.7.6的,django是1.4的 ,看起来很方便,但是是那么完美吗?

开始跟着The Django Book 学,到了启用admin界面时,总是给个黑白的,不是彩色的,漂亮的界面:


学习阶段,直接使用 manage.py runserver命令了。

看了很多百度的文章,包括django的官方文档,都是说只要:

  • settings中的debug要为True
  • settings中INSTALLED_APPS中要有 'django.contrib.staticfiles'
就可以了。
而STATIC_URL = '/static/' 这句,默认就有。

django说,开发服务器会自动解析静态文件。

折腾1天,试了各种参数,如STATIC_ROOT(证明这个只对用manage.py collectstatic 起作用),一度怀疑要使用 django-admin.py runserver来运行开发服务器。

其实在浏览器地址栏里打 http://127.0.0.1:8000/admin/后,点菜单,开发服务器是有错误提示的:


python2.x 默认编码是ascii。 这个开始真不懂。
觉着可能是开发服务器读取静态文件,而模块里按utf-8处理,出现字符串混乱,而导致css、js及image文件找不到。
就打算看看让python 2.x运行在utf-8下。

百度了个文章:
可以手动建一个sitecustomize.py强制python使用utf-8。
开始看的文章里面把utf-8写成utf8了,照着弄错了。
python的编码是utf8,自然不对,还是错误。

可以看这个博客,他这个是对的。

看到这文章晚了,把bitnami的集成包卸载了。
安装了Python 3.33和django 1.6.1,Python自带sqlite3,学习是足够了。

Python3默认就是utf-8了,大家都统一了,世界终于一片清凉:



运行正常的开发服务器:


实际,python 2.x如果运行在utf-8下也应该可以。
反正2.7.6的版本,语法与3.x的差不多了,更换版本没有

跨平台的东西,比微软的就是复杂,一个编码就让人头疼半天。
而且版本之间的一致性要特别加以小心。
这次没有好好看错误信息,也走了很多弯路。

学习的过程中,会有很小的拦路虎,要战胜它,一遇到困难就放弃,就总是浅尝辄止,而不能学会一门技能。微笑微笑微笑


对于   File "D:\Python\Lib\mimetypes.py", line 249, in enum_types     ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)


This is a bug in mimetypes, triggered by bad data in the registry. (рєфшю/AMR is not at all a valid MIME media type.)


ctype is a registry key name returned by _winreg.EnumKey, which mimetypes is expecting to be a Unicode string, but it isn't. Unlike _winreg.QueryValueEx, EnumKey returns a byte string (direct from the ANSI version of the Windows API; _winreg in Python 2 doesn't use the Unicode interfaces even though it returns Unicode strings, so it'll never read non-ANSI characters correctly).


So the attempt to .encode it fails with a Unicode​Decode​Error trying to get a Unicode string before encoding it back to ASCII!

try:    ctype = ctype.encode(default_encoding) # omit in 3.x!except UnicodeEncodeError:    pass




These lines in mimetypes should simply be removed.
注释掉上面几行。


验证可以用。

0 1
原创粉丝点击