我使用 django 1.8 创建了一个“testproject”。CSS、JS、图像不工作。我在项目文件夹之外包含了模板、css和js文件夹,如下所示:
我的项目文件夹是:/home/myuser/testproject/
我的媒体文件夹是:/var/www/test_media/
模板(文件夹)
css(文件夹)
js(文件夹)
设置.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/'
索引.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>
视图.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))