我无法让我的静态文件工作。我花了 6 个小时查看有关此主题的许多帖子,但我仍然做错了什么。请帮忙。这是我的设置:
projectfiles
|
|-----myproject
| |
| |-----static
| | |
| | |-----css
| | |-----js
| |-----__init__.py
| |-----settings.py
| |-----urls.py
| |-----wsgi.py
|
|-----myapp
|
|-----templates
settings.py
import os
SITE_ROOT = (os.path.realpath(os.path.dirname(__file__))).replace('\\','/')
DEBUG = True
MEDIA_ROOT = (os.path.join(SITE_ROOT, '/static')).replace('\\','/')
MEDIA_URL = '/static/'
STATIC_ROOT = ''
STATIC_URL = ''
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
urls.py
urlpatterns = patterns('',
(r'^myurl/$', myview),
)
from myproject.settings import DEBUG
if DEBUG:
urlpatterns += patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': 'static'}))
mytemplate.html
...
<head>
<link src="css/mycss.css" rel="stylesheet" type="text/css"/>
...
该应用程序运行良好,但与我的 css 或 javascripts 没有连接。我错过了什么?
任何帮助将不胜感激。
Update:
`STATIC_ROOT='C:/path/to/myproject/static/'
STATIC_URL='/static/'
TEMPLATE_CONTEXT_PROCESSORS=('...static', )
STATICFILES_DIRS=('C:/absolute/path/to/myapp/static',)
STATICFILES_FINDERS=('...FileSystemFinder','...AppDirectoriesFinder',)
INSTALLED_APPS = (...,'django.contrib.staticfiles',)
#does not work with or without this:
urlpatterns += staticfiles_urlpatterns()
#views now rendered like this:
myview(request):
...
return render_to_response('template.html',{'a': a},context_instance =RequestContext(request))
#template.html
<link src="{{STATIC_URL}}css/mycss.css"/>