tch*_*ore 51 python django static-files
我runserver
在我的本地计算机(Mac OS X)上运行Django的开发服务器()并且无法加载CSS文件.
以下是settings.py中的相关条目:
STATIC_ROOT = '/Users/username/Projects/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/Users/thaymore/Projects/mysite/cal/static',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
INSTALLED_APPS = (
# other apps ...
'django.contrib.staticfiles',
)
Run Code Online (Sandbox Code Playgroud)
在我的views.py中,我正在请求上下文:
return render_to_response("cal/main.html",dict(entries=entries),context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
在我的模板中{{ STATIC_URL }}
正确呈现:
<link type="text/css" href="{{ STATIC_URL }}css/main.css" />
Run Code Online (Sandbox Code Playgroud)
变成:
<link type="text/css" href="/static/css/main.css"/>
Run Code Online (Sandbox Code Playgroud)
这是文件实际所在的位置.我还跑去collectstatic
确保收集所有文件.
我的urls.py中还有以下几行:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
Run Code Online (Sandbox Code Playgroud)
我是Django的新手,所以我可能会错过一些简单的东西 - 感谢任何帮助.
GDo*_*orn 71
请仔细阅读:https: //docs.djangoproject.com/en/dev/ref/contrib/staticfiles/
是django.contrib.staticfiles
你INSTALLED_APPS
的settings.py
?
是DEBUG=False
吗?如果是这样,您需要runserver
使用--insecure
参数调用:
python manage.py runserver --insecure
Run Code Online (Sandbox Code Playgroud)
collectstatic
与通过开发服务器提供文件无关.它用于在一个位置收集静态文件,STATIC_ROOT
供Web服务器查找.事实上,collectstatic
使用你的STATIC_ROOT
设置运行到一个路径STATICFILES_DIRS
是一个坏主意.您应该仔细检查以确保您的CSS文件现在都存在.
All*_*ітy 22
使用Django 1.11.x +,您必须在settings.py
as中配置静态文件,
STATIC_URL = '/static/' # the path in url
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
Run Code Online (Sandbox Code Playgroud)
并将其与静态模板标签一起使用,
{% load static %}
<link rel="stylesheet" href="{% static 'css/bootstrap.css' %}">
Run Code Online (Sandbox Code Playgroud)
另一个简单的尝试是停止,然后重新启动服务器,例如
$ python manage.py runserver
Run Code Online (Sandbox Code Playgroud)
我查看了其他答案,但重启服务器对我有用.