我正在构建一个简单的Django应用程序,我的CSS没有出现在开发服务器上,即使它只是打开在我的浏览器中链接到它的HTML文件也能正常工作。我已经调查了关于SO的其他相关问题,以及关于提供静态文件的官方教程,但仍然无法弄清楚如何使我的CSS工作。
我是新手,所以请以最简单的方式提出建议(但不要简单)。
谢谢!
目录结构:
uber
uber
static
css
style.css
templates
home.html
settings.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'
)
urls.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 with link:
<head>
<link rel='stylesheet' href='/static/css/style.css' type='text/css' media='screen'>
</head>
答案 0 :(得分:0)
dev服务器只能通过staticfiles框架自动提供文件。
您应该只将媒体文件夹添加到STATICFILES_DIRS
以利用该框架。
但是,如果您想在开发中MEDIA_ROOT
处MEDIA_URL
投放,则必须设置一个自己提供的URLConf。
https://docs.djangoproject.com/en/dev/howto/static-files/#staticfiles-other-directories
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT,
}),
)