django html 加载静态 js css(windows) pycharm

本文详细介绍了Django项目中如何配置静态文件路径、使用模板引擎,并解决静态URL为空的问题。通过实例展示了如何正确引入静态资源,确保前端样式和脚本正常加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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))
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值