我创建了一个" testproject"使用django 1.8。 CSS,JS,Images无效。我在项目文件夹之外包含了模板,css和js文件夹,如下所示:
我的项目文件夹是:/home/myuser/testproject/
我的媒体文件夹是:/var/www/test_media/
模板(文件夹)
css(文件夹)
js(文件夹)
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = '#tg20j5v6odye)p*pd9u=+^1xybqlmicvuq+#&m*&c(dcuk6n)'
DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test',
)
MIDDLEWARE_CLASSES = (
'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',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'testproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/var/www/test_media/template'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.media',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'testproject.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Calcutta'
USE_I18N = True
USE_L10N = True
USE_TZ = False
STATIC_URL = '/static/'
MEDIA_ROOT = '/var/www/test_media/'
MEDIA_URL = 'test_media/'
index.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="{{MEDIA_URL}}css/style.css">
<script src='{{MEDIA_URL}}jquery/jquery.js'></script>
<script src="{{MEDIA_URL}}js/index.js"></script>
<meta charset="UTF-8">
<title>TEST PROJECT</title>
</head>
<body>
<div class="wrapper">
<h1>Welcome </h1>
<form class="form">
<input type="text" placeholder="Username">
<input type="password" placeholder="Password">
<button type="submit" id="login-button">Login</button>
</form>
</div>
</body>
</html>
views.py
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.template import RequestContext
def logIn(request):
return render_to_response("index.html", context_instance=RequestContext(request))
答案 0 :(得分:1)
您需要先设置STATIC_ROOT和STATICFILES_DIRS来设置和使用静态文件,以说明文件的位置和应该去的地方
答案 1 :(得分:1)
我收到了媒体文件!! ..我在settings.py中添加了
STATICFILES_DIRS = ( os.path.join(BASE_DIR,"static"),
'/var/www/test_media/', )
&安培;在index.html中更改
{% load staticfiles %}
<link rel="stylesheet" href="{% static "css/style.css" %}" >
<script src="{% static "jquery/jquery.js" %}"></script>