gpt4 book ai didi

html - 链接到 Django HTML 模板中的 CSS 文件

转载 作者:行者123 更新时间:2023-11-28 13:24:09 24 4
gpt4 key购买 nike

我正在构建一个简单的 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'
)

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>

最佳答案

开发服务器仅通过静态文件框架自动提供文件。

您实际上应该只将您的媒体文件夹添加到您的 STATICFILES_DIRS 以利用该框架。

但是,如果你想在开发中为 MEDIA_URL 提供 MEDIA_ROOT,你必须自己设置一个 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,
}),
)

关于html - 链接到 Django HTML 模板中的 CSS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14369984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com