我花了整整一天的时间来弄清楚为什么静态文件尽管被正确引用却没有出现在网站上。我的基本模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}Sinergia Creativa{% endblock %}</title>
{% load staticfiles %}
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"/>
<link href='https://fonts.googleapis.com/css?family=Satisfy' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="{% static 'publicidad/style.css' %}"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#topNavBar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'publicidad:index' %}">Sinergia Creativa</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="">
<a href="#">Promociones</a>
</li>
<li class="">
<a href="#">Contacto</a>
</li>
</ul>
</div>
</div>
</nav>
</div>
</div>
{% block body %}
{% endblock %}
</body>
</html>
我的settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'publicidad.apps.PublicidadConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'sinergia_creativa.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'sinergia_creativa.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
我有另一个项目以相同的方式配置并且可以正常工作,所以我很困惑。
我的主要问题是,尽管实际引用了这些静态文件,却没有任何问题,但它们根本没有加载。我在manage.py shell中使用了findstatic,它在正确的位置找到了它们。我用Firebug看看被引用的是正确的路径
<link href="/static/publicidad/style.css" type="text/css" rel="stylesheet">
Reload the site to obtain the source code of: http://localhost:800/static/publicidad/style.css
我不知道该怎么办在这些设置下,其他项目运行良好。让我知道您是否需要该项目的任何其他信息。
干杯,Datkros。
最佳答案
我在settings.py中看不到定义静态文件。将更多设置添加到settings.py(在我的情况下,静态文件夹在我的项目中)
import os.path
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
# ('static', os.path.join(BASE_DIR, 'static')),
os.path.join(BASE_DIR, 'static'),
)
关于python - Django:尽管引用正确,静态文件仍未显示在网站中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36393307/