如何通过Django使用本地css/js文件

作者:虚生 时间:2022-04-28 22:04:33 

这篇文章主要介绍了如何通过Django使用本地css/js文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

在网上看了很多说Django如何使用本地css/js的文章, 很多都是说的不是很清楚。

今天终于自己来验证一个能用的了, 记录下

在manager.py同层级下创建static文件夹, 里面放上css , js, image等文件或者文件夹

我的文件夹tree:


➜ FileService git:(master) ✗ tree
.
├── 2kill_port.sh
├── FileService
│  ├── __init__.py
│  ├── __pycache__
│  │  ├── __init__.cpython-37.pyc
│  │  ├── settings.cpython-37.pyc
│  │  ├── urls.cpython-37.pyc
│  │  └── wsgi.cpython-37.pyc
│  ├── settings.py
│  ├── urls.py
│  └── wsgi.py
├── db.sqlite3
├── en_bg.jpg
├── fileoperation
│  ├── __init__.py
│  ├── __pycache__
│  │  ├── __init__.cpython-37.pyc
│  │  ├── admin.cpython-37.pyc
│  │  ├── models.cpython-37.pyc
│  │  └── views.cpython-37.pyc
│  ├── admin.py
│  ├── apps.py
│  ├── migrations
│  │  ├── __init__.py
│  │  └── __pycache__
│  │    └── __init__.cpython-37.pyc
│  ├── models.py
│  ├── templates
│  │  ├── bg_homg.html
│  │  └── home.html
│  └── views.py
├── files
│  ├── 11.log
│  ├── 22.log
│  ├── 44.log
│  ├── th.jpeg
│  ├── �\217�\225快�\205�\ 2020-01-08\ �\213�\215\2101.50.03.png
│  └── �\235�\231��\212��\224\200�\207��\201.pdf
├── kill_port.sh
├── manage.py
└── static
 ├── images
 │  └── en_bg.jpg
 └── style
   └── style.css

然后只需在FileService/settings.py中进行设置就行, 在末尾添加以下代码


STATIC_URL = '/static/'
STATICFILES_DIRS=[
 os.path.join(BASE_DIR,'static')
]

最后只需要在使用的html文件中通过以下方式导入:


<head>
 <link rel="stylesheet" type="text/css" href="../static/style/style.css" rel="external nofollow" />
 <meta charset="UTF-8">
 <title>文件传输</title>
</head>

来源:https://www.cnblogs.com/dylancao/p/12170390.html

标签:Django,css,js,文件
0
投稿

猜你喜欢

  • Python matplotlib 绘制双Y轴曲线图的示例代码

    2023-04-14 11:07:27
  • linux ubuntu中安装、卸载和删除python-igraph的方法教程

    2021-01-10 19:54:02
  • Python数据类型学习笔记

    2023-02-09 13:42:39
  • Python 调用有道翻译接口实现翻译

    2023-03-11 06:35:34
  • Python Flask 转换器的使用详解

    2023-06-30 15:42:09
  • Python使用Beautiful Soup实现解析网页

    2022-11-21 19:17:22
  • 卓越网的配送服务让我很不满意

    2009-03-19 13:49:00
  • Python常用数据类型之间的转换总结

    2023-06-21 10:06:21
  • Python中random模块常用方法的使用教程

    2023-12-03 16:30:11
  • 不是原型继承那么简单!prototype的深度探索

    2008-03-07 12:42:00
  • python配置虚拟环境步骤

    2023-10-22 09:34:51
  • python 重定向获取真实url的方法

    2022-10-25 03:24:46
  • Python高阶函数与装饰器函数的深入讲解

    2023-10-04 12:42:41
  • pycharm运行出现ImportError:No module named的解决方法

    2022-09-10 18:06:23
  • 对python抓取需要登录网站数据的方法详解

    2023-01-18 04:58:41
  • PHP使用观察者模式处理异常信息的方法详解

    2023-10-19 15:53:30
  • PHP单例模式简单用法示例

    2023-11-18 19:45:41
  • Django request.META.get()获取不到header头的原因分析

    2021-03-16 00:22:39
  • 解决Django中checkbox复选框的传值问题

    2023-09-11 17:15:22
  • 你喜欢篮球吗?Python实现篮球游戏

    2023-10-26 00:28:36
  • asp之家 网络编程 m.aspxhome.com