/project_root/
manage.py
/project/
/static/
/css/
/js/
/img/
/media/
settings.py
urls.py
这是我的文件夹结构。我的 settings.py 是这样的:
这是我的设置.py:
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'staticfiles'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
网址.py :
urlpatterns = patterns('',
url(r'^$', 'employee.views.index'),
url(r'^admin/', include(admin.site.urls)),
)+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += staticfiles_urlpatterns()
模板:
{% load static %}
{% load staticfiles %}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="{{ STATIC_URL }}img/logo48.png" rel="SHORTCUT ICON" />
<link href="{{ STATIC_URL }}css/master.css" rel="stylesheet" type="text/css" media="screen">
<link href="{{ STATIC_URL }}css/custom.css" rel="stylesheet" type="text/css" media="screen">
在查看源代码中我可以看到:
<link href="/static/css/master.css" rel="stylesheet" type="text/css" media="screen">
<link href="/static/css/custom.css" rel="stylesheet" type="text/css" media="screen">
<link href="/static/css/fixes.css" rel="stylesheet" type="text/css" media="screen">
但是我的模板文件中没有加载 css。请指导我。
最佳答案
请注意,如果您使用的是内部 Django 开发服务器,则此答案不适用。
您是否在 Apache 设置中为 /static/
添加了别名?否则,对该文件夹的任何调用都将导致 404 错误,这意味着您的 CSS 将无法加载。这是我的意思的一个例子:
<VirtualHost *:80>
ServerName www.example.com
WSGIScriptAlias / "/path/to/project/project/wsgi.py"
DocumentRoot "/path/to/project"
<Directory "/path/to/project">
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
Alias /media/ "/path/to/project/media/"
<Directory "/path/to/project/media/">
Order deny,allow
Allow from all
</Directory>
Alias /static/ "/path/to/project/static/"
<Directory "/path/to/project/static/">
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
关于python - 无法在我的 django 1.5 模板中加载 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18693741/