【发布时间】:2013-12-16 21:51:55
【问题描述】:
我最近将我的项目环境升级到了 django 1.6。升级后发现现有项目中的所有静态文件都不起作用了,即使按照教程新建一个项目,静态文件仍然不起作用。谁能帮帮我?
这是我的项目结构
-mysite
manage.py
-mysite
urls.py
views.py
settings.py
__init__.py
wsgi.py
-static
jqury.js
settings.py 看起来像这样
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
)
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'WorldCup\\templates'),
)
TEMPLATE_CONTEXT_PROCESSORS = [
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'WorldCup\\static'),
)
print STATICFILES_DIRS
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
我打印staticfiles_dirs,结果是('G:\django_project\mysite\mysite\static',),它正确指向我的静态文件夹。
html 文件如下所示:
{% load staticfiles %}
<script type="text/javascript" src="{% static "WorldCup/jquery-1.10.2.js" %}"
<body>
hello world======{% static "WorldCup/jquery-1.10.2.js" %}=======
</body>
页面显示"hello world======/static/jquery-1.10.2.js=======",看起来是正确的。
但控制台抱怨:
Traceback (most recent call last):
File "D:\Python27\lib\wsgiref\handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "D:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 68, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "D:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 206, i
n __call__
response = self.get_response(request)
File "D:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 58, in get_response
return self.serve(request)
File "D:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", l
ine 51, in serve
return serve(request, self.file_path(request.path), insecure=True)
File "D:\Python27\lib\site-packages\django\contrib\staticfiles\views.py", line
41, in serve
return static.serve(request, path, document_root=document_root, **kwargs)
File "D:\Python27\lib\site-packages\django\views\static.py", line 61, in serve
content_type, encoding = mimetypes.guess_type(fullpath)
File "D:\Python27\lib\mimetypes.py", line 297, in guess_type
init()
File "D:\Python27\lib\mimetypes.py", line 358, in init
db.read_windows_registry()
File "D:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
for subkeyname in enum_types(hkcr):
File "D:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb9 in position 0: ordinal
not in range(128)
[01/Dec/2013 07:42:33] "GET /static/jquery-1.10.2.js HTTP/1.1" 500 59
即使我更改为图像文件而不是 js 文件,我仍然会收到这些错误....
【问题讨论】:
-
静态文件的完整路径中是否可能包含非 ASCII 字符?
-
你确定这是正确的路径吗?也许它实际上应该是
G:\django_project\mysite\static(省略一个mysite)? -
其实我想知道您的 Windows 注册表中是否没有非 ASCII 字符!回溯似乎正在从中读取 mimetype 信息。我怀疑你发现了一个非常微妙的错误,它只发生在 Windows 上的 Python 2.x 中。
-
to yuvi:js文件的物理路径是“G:\django_project\mysite\mysite\static*.js”,我不认为问题出在这里
-
致 Andrew Gorcester:我想问我如何查看我的窗口是否有 ASCII 字符?如果我没有 ASCII 字符,我该如何解决?
标签: django windows python-2.7 static-files