Django模板中使用css, javascript
(r'^css/(?P<path>.*)$', 'django.views.static.serve', {'document_root': '/var/www/django-demo/css'}),
(r'^js/(?P</path><path>.*)$', 'django.views.static.serve', {'document_root': '/var/www/django-demo/js'}),
(r'^images/(?P</path><path>.*)$', 'django.views.static.serve', {'document_root': '/var/www/django-demo/images'}),
模板中使用下述方式即可:
<link href="/css/demo.css" type="text/css" rel="stylesheet">
注:可采用os.path.dirname(globals()["__file__"])来获得当前文件所在路径,比如
(r'^css/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.dirname(globals()["__file__"])+'/css'}),
可以使用os.path.abspath()函数返回此路径的绝对路径。
