我正在构建一个简单的 Django 应用程序,并且我的 CSS 没有显示在开发服务器上,即使我只是在浏览器中打开链接到它的 HTML 文件,它确实可以工作。我已经调查了关于 SO 的其他相关问题,以及关于提供静态文件的官方教程,但仍然无法弄清楚如何让我的 CSS 工作。
我是新手,所以请尽可能以最简单的方式提供建议(但不是更简单)。
谢谢!
目录结构:
uber
uber
static
css
style.css
templates
home.html
设置.py:
MEDIA_ROOT = '/Users/pavelfage/Desktop/Hacking/django/uber/static/'
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/static/'
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = ''
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
#'/Users/pavelfage/Desktop/Hacking/django/uber/uberpoll/static',
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = '(au)ipe++*c6e6r1s00z9d99qgsx!loqh*-!hcqys=6&bjk#*a'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'uber.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'uber.wsgi.application'
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
'/Users/pavelfage/Desktop/Hacking/django/uber/templates',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# 'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'uberpoll'
)
网址.py:
from django.conf.urls import patterns, include, url
from uber import settings
urlpatterns = patterns('',
# Examples:
url(r'^$', 'uberpoll.views.home'),
# url(r'^uber/', include('uber.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT})
)
带有链接的 HTML 头:
<head>
<link rel='stylesheet' href='/static/css/style.css' type='text/css' media='screen'>
</head>