我是django的新手。我一直在尝试使用django1.11开发一个网站。但我在某个时候陷入困境。静态文件不是通过模板传递的。任何帮助!
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'agr8w4(pcdz077#8n2ow1z8_@e-%6(evtw((-3g$&$_wf1&!@1'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'photo',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'pro.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
'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 = 'pro.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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 files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static", "static_root")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static", "our_static" ),
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder'
)
MEDIA_URL ='/media/'
MEDIA_ROOT =os.path.join(BASE_DIR, "media_root")
urls.py
from django.conf import settings
from django.conf.urls import include,url
from django.conf.urls.static import static
from django.contrib import admin
from photo.views import home
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
#urlpatterns += staticfiles_urlpatterrens()
urlpatterns = [
url(r'^$',home,name='home'),
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
views.py
from django.shortcuts import render
def home(request):
return render(request,"home.html",{})
home.html的
{% load static %}
<!doctype html>
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>Photography</title>
<link rel="stylesheet" href="{% static '/our_static/css/main.css' %}" type="text/css">
</head>
<body>
<h1>Something here</h1>
<h2>Anything!</h2>
<section>
<div>
<nav>
<ul>
<li><a href='#home'>Home</a></li>
<li><a href='#aboutus'>AboutUs</a></li>
<li><a href='#portfolio'>Portfolio</a></li>
<li><a href='#services'>Services</a></li>
<li><a href='#contact'>Contact</a></li>
</ul>
</nav>
</div>
</section>
<img src= "{% static '/our_static/img/a.jpg' %}" height="500" width="500" >
<footer>
©Copyrights@2017. All rights reserved.
</footer>
</body>
</html>
的main.css
*{
margin: 0px;
padding: 0px;
}
header, nav, footer {
display: block;
}
body{
text-align: center;
}
header{
color: #777777;
}
目录结构就像这样!
python manage.py collectstatic
正常运行,没有任何错误,文件也被复制到static_root。但是在运行python manage.py runserver
时它没有在页面上加载
这是我跑步时得到的。
答案 0 :(得分:1)
从网址中删除our_static
。由于static
标签取代了它,因此不需要它。
<link rel="stylesheet" href="{% static 'css/main.css' %}" type="text/css">
<img src= "{% static 'img/a.jpg' %}" height="500" width="500" >