0

シンプルな Django アプリを作成しています。CSS にリンクしている HTML ファイルをブラウザーで開いただけでは機能しますが、CSS が開発サーバーに表示されません。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>
        <link rel='stylesheet' href='/static/css/style.css' type='text/css' media='screen'>
</head>
4

1 に答える 1

0

開発サーバーは、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,
        }),
   )
于 2013-01-17T00:01:18.933 に答える