首页 > django中怎么载入css等静态文件

django中怎么载入css等静态文件

新手,在网上找了很多方法都没有成功,已经研究好几天了,求大家解救
window7 python2.7 django1.6.2
目录结构:

website
|-sites
| |-__init__.py
| |-manage.py
| |-settings.py
| |-urls.py
|-templates
| |-...(html files)
| |-static
|   |-css
|   |-js

settings.py中static变量的设置:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_PATH = os.path.join(BASE_DIR, 'templates/static').replace('\\','/')

urls.py中urlpatterns的设置

urlpatterns = patterns('',
    url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_PATH}),
)

模板文件中的引用方式

<script src="/static/js/jquery.js"></script>
<link href="/static/css/css.css" rel="stylesheet" type="text/css">

无效。
另:如果想在debug=false下,又要怎么设置?



后又尝试:
urls.py中urlpatterns的设置

urlpatterns = patterns('',
    url(r'^media/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_PATH}),
)

static换为media
模板文件中的引用方式

<script src="/media/js/jquery.js"></script>
<link href="/media/css/css.css" rel="stylesheet" type="text/css">

同样,static换为media

结果好像是找到文件了,但是又出现了别的错误:

Exception Type: UnicodeDecodeError
Exception Value:
'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)

我的文件都是utf-8编码的,绝对不是ascii,而且文件里也没有出现中文。
求解答!

错误信息:

UnicodeDecodeError at /statics/css/test.css
'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
Request Method: GET
Request URL: http://127.0.0.1:8000/statics/css/test.css
Django Version: 1.6.2
Exception Type: UnicodeDecodeError
Exception Value:
'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)
Exception Location: C:\Python27\lib\mimetypes.py in enum_types, line 249
Python Executable: C:\Python27\python.exe
Python Version: 2.7.6
Python Path:
['D:\Project\django\site01\sites',
'C:\Windows\system32\python27.zip',
'C:\Python27\DLLs',
'C:\Python27\lib',
'C:\Python27\lib\plat-win',
'C:\Python27\lib\lib-tk',
'C:\Python27',
'C:\Python27\lib\site-packages']
Server time: 星期六, 26 四月 2014 23:48:08 +0800
Unicode error hint

The string that could not be encoded/decoded was: .������
Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) ...
▶ Local vars
C:\Python27\lib\site-packages\django\views\static.py in serve
content_type, encoding = mimetypes.guess_type(fullpath) ...
▶ Local vars
C:\Python27\lib\mimetypes.py in guess_type
init() ...
▶ Local vars
C:\Python27\lib\mimetypes.py in init
db.read_windows_registry() ...
▶ Local vars
C:\Python27\lib\mimetypes.py in read_windows_registry
for subkeyname in enum_types(hkcr): ...
▶ Local vars
C:\Python27\lib\mimetypes.py in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x! ...
▶ Local vars


自己解决了!
settings.py中的设置:

STATICFILES_DIRS = (
    'templates/static'
)

urls.py中设置:

url(r'^staticfiles/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATICFILES_DIRS, 'show_indexes': True}),

html中引用方式:

<link href="/staticfiles/css/test.css" rel="stylesheet" type="text/css">

这样设置完,文件是找到了,但是会出现编码错误,错误行是:

C:\Python27\lib\mimetypes.py in enum_types, line 249

在网上找了半天,找到了解决编码错误的办法,
在mimetypes.py文件中,添加

reload(sys)
sys.setdefaultencoding('gb18030')

解决!


模版中:使用{{STATIC_URL}} 例如<link href="{{STATIC_URL}}css/bootstrap.css" rel="stylesheet">
debug=False的情况下
请用web服务器解析静态文件,比如apache或者nginx之类的:

 # Django media
location /media  {
    alias /var/sites/django_www/yunshuba/media;  # your Django project's media files - amend as required
}

location /static {
    alias /var/sites/django_www/yunshuba/static; # your Django project's static files - amend as required
}
【热门文章】
【热门文章】