Django<加载static目录下的CSS-JS>

本文详细介绍了解决Django项目中加载静态文件如JS和CSS的问题。通过设置STATIC_URL、STATIC_ROOT及STATICFILES_DIRS,并使用collectstatic命令收集静态文件,最终实现在HTML中正确引用这些资源。

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

博主之前没有接触过Django,最近有一个项目需求要做一个可视化的展示,但是一直无法加载static文件下的JS和CSS,经过一番折腾,最后终于搞定了。下面是完整的过程:

1.我的项目结构:

这里写图片描述

2.在与settings.py文件的统计目录下,新建static文件夹,然后在新建两个子目录(static_dirs、static_root),然后在settings.py文件中添加以下代码:

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'mysite', 'static', 'static_root')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'mysite', 'static', 'static_dirs'),
)

3.在pycharm的终端中运行shell

 python manage.py collectstatic

这里写图片描述
不出意外的话,static/static_root下会多出这么写文件

这里写图片描述

接下来将需要使用到的js和css文件放到对应的目录下,如下图:

这里写图片描述

在html中引入这些js、css

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" type="text/css" href="{% static 'css/bootstrap.css' %}">
    <script src="{% static 'js/jquery-3.2.1.min.js' %}"></script>
    <script src="{% static 'js/bootstrap.js' %}"></script>
</head>
<body>
<!-- 标准的按钮 -->
<button type="button" class="btn btn-default">默认按钮</button>
<!-- 提供额外的视觉效果,标识一组按钮中的原始动作 -->
<button type="button" class="btn btn-primary">原始按钮</button>
<!-- 表示一个成功的或积极的动作 -->
<button type="button" class="btn btn-success">成功按钮</button>
<!-- 信息警告消息的上下文按钮 -->
<button type="button" class="btn btn-info">信息按钮</button>
<!-- 表示应谨慎采取的动作 -->
<button type="button" class="btn btn-warning">警告按钮</button>
<!-- 表示一个危险的或潜在的负面动作 -->
<button type="button" class="btn btn-danger">危险按钮</button>
<!-- 并不强调是一个按钮,看起来像一个链接,但同时保持按钮的行为 -->
<button type="button" class="btn btn-link">链接按钮</button>
<table class="table">
    <caption>基本的表格布局</caption>
   <thead>
      <tr>
         <th>名称</th>
         <th>城市</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>Tanmay</td>
         <td>Bangalore</td>
      </tr>
      <tr>
         <td>Sachin</td>
         <td>Mumbai</td>
      </tr>
   </tbody>
</table>
</body>
</html>

然后重启App, 观察浏览器的效果,可以看到效果已经加载进来了:
这里写图片描述

至此,相应的文件已经加载进来了。待我有时间好好学一学Django之后,再来解释其中的奥秘。

这段代码看看有没有问题{% load static i18n %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <!-- plugin css --> <link href="{% static 'zadmin/libs/dropzone/min/dropzone.min.css' %}" rel="stylesheet" type="text/css"/> <title>Title</title> </head> <body> <form class="dropzone" action="{% url 'zadmin:filetext' %}" method="post" id="my-awesome-dropzone" enctype="multipart/form-data"> {% csrf_token %} {{ form.as_p }} <button type="submit" id="upload-btn">上传</button> {#<input type="file" name="file" />#} </form> <!-- Plugins js--> <script src="{% static 'zadmin/libs/dropzone/min/dropzone.min.js' %}"></script> <script type="text/javascript"> Dropzone.options.myDropzone = { // Prevents Dropzone from uploading dropped files immediately autoProcessQueue : false, paramName: "file", url: "{% url 'zadmin:filetext' %}", addRemoveLinks: true, maxFiles: 10, maxFilesize: 20, acceptedFiles: ".jpg,.gif,.png", previewsContainer: "#adds", parallelUploads: 3, dictMaxFilesExceeded: "您最多只能上传10个文件!", dictResponseError: '文件上传失败!', dictInvalidFileType: "你不能上传该类型文件,文件类型只能是*.jpg,*.gif,*.png。", init : function() { const submitButton = document.querySelector("#upload-btn"); myDropzone = this; submitButton.addEventListener("click", function() { myDropzone.processQueue(); // Tell Dropzone to process all queued files. }); // You might want to show the submit button only when // files are dropped here: this.on("addedfile", function(file, response) { // 显示提交按钮在这里和/或通知用户点击它。 }); this.on("success", function (file, response) { // 处理文件上传成功的回调 console.log("文件上传成功!"); }); this.on("error", function (file, response) { // 处理文件上传失败的回调 console.log("文件上传失败!"); }); } }; </script> </body> </html>
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值