settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static').replace('\\','/')
STATICFILES_DIRS =(
("css", os.path.join(STATIC_ROOT,'css').replace('\\','/')),
("js", os.path.join(STATIC_ROOT,'js').replace('\\','/')),
)
STATIC_URL = '/static/'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
#'DIRS': [],
'DIRS':TEMPLATE_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',
'django.core.context_processors.static',
],
},
},
]
其中’django.core.context_processors.static’
启用staticfiles
templates/table.html
<head>
<title>Changhong ID Search</title>
<link href="{{STATIC_URL}}css/bootstrap.min.css" rel="stylesheet">
<link href="{{STATIC_URL}}css/bootstrap.css" rel="stylesheet">
<link href="{{STATIC_URL}}css/bootstrap-theme.css" rel="stylesheet">
<link href="{{STATIC_URL}}css/bootstrap-theme.min.css" rel="stylesheet">
<script src="{{STATIC_URL}}js/jquery-2.1.1.min.js"></script>
<script src="{{STATIC_URL}}js/bootstrap.min.js"></script>
</head>
从浏览器访问发现STATIC_URL为空,应该是view方法中没有传contenxt_instance
加上之后就能通过{{STATIC_URL}}访问/static/
render_to_response('table.html',locals(),context_instance=RequestContext(request))
本文详细介绍了Django项目中如何配置静态文件路径、使用模板引擎,并解决静态URL为空的问题。通过实例展示了如何正确引入静态资源,确保前端样式和脚本正常加载。
 pycharm&spm=1001.2101.3001.5002&articleId=46629247&d=1&t=3&u=87653879e7c34b068ce61864073cb67a)
1436

被折叠的 条评论
为什么被折叠?



