Django 开发模式下静态资源加载

Django Debug=True模式下静态资源加载

本项目的结构如下:

在这里插入图片描述

  1. 首先在setting.py中设置如下:
DEBUG = True
#STATIC_URL用来引用静态文件,也就是渲染之后HTML中静态文件的前缀,若STATIC_URL="/sta/",则访问路径:http://localhost:8000/sta/
STATIC_URL="/static/"
#Static files (CSS, JavaScript, Images)
#https://docs.djangoproject.com/en/3.1/howto/static-files/
#python manage.py collectstatic 会把静态目录下的所有文件拷贝至 STATIC_ROOT 目录。
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')

#STATICFILES_DIRS = (
#('css', os.path.join(STATIC_ROOT, 'css')),
#('images', os.path.join(STATIC_ROOT, 'images')),
#('js', os.path.join(STATIC_ROOT, 'js')),
#)
# 若在static文件夹中新建此三个文件夹,同时加入下面代码,当执行python manage.py collectstatic 会把这些目录下的所有文件拷贝至STATIC_ROOT 目录
# STATICFILES_DIRS = [
#
#      os.path.join(STATIC_ROOT, 'css'),
#      os.path.join(STATIC_ROOT, 'images'),
#      os.path.join(STATIC_ROOT, 'js'),
# ]
  1. html中引用

{% load static  %}
<html lang="en" xmlns="http://www.w3.org/1999/html">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'weapp/css/index.css' %}">
      <link rel="stylesheet" href="{% static 'weapp/css/head.css' %}">
    <title>花影世界</title>
  </head>
  <body>
    <img class="index" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fwallpaper%2F2018-05-31%2F5b0fa0cd12041.jpg%3Fdown&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1621159795&t=ccbb1cbf0fb117e6b8be90360880e717" class="img-fluid" alt="Responsive image">

    <img class="head" src="{% static 'weapp/1.jpg' %}">
    <video class="index" src="{% static 'weapp/new.mp4' %}" controls="controls"></video>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>

在这里插入图片描述
3.定义路由
在这里插入图片描述
4. 定义view函数
在这里插入图片描述
5. 启动Django,即可正常访问静态资源。

ps:此上方法只适用于开发环境,当Debug=False时,将不在加载静态资源。生产环境中可以先在本地请执行python manage.py collectstatic 系统会将所有静态资源复制到STATIC_ROOT 目录(注意此目录)下,然后利用nginx或Apach进行静态资源代理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果 Django 无法加载静态资源,可能是因为以下原因之一: 1. STATICFILES_DIRS 设置不正确:在 settings.py 文件中,确保 STATICFILES_DIRS 设置为包含你静态文件目录的路径。例如,如果你的静态文件位于 my_project/static/ 中,则应将其添加到 STATICFILES_DIRS 中:`STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]`。 2. STATIC_ROOT 设置不正确:在 settings.py 文件中,确保 STATIC_ROOT 设置为你希望 Django 收集所有静态文件的目录。例如,如果你希望 Django静态文件收集到 my_project/staticfiles/ 目录中,则应将其设置为:`STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')`。 3. STATICFILES_FINDERS 设置不正确:在 settings.py 文件中,确保 STATICFILES_FINDERS 包含正确的查找器。通常情况下,你需要包含 'django.contrib.staticfiles.finders.FileSystemFinder' 和 'django.contrib.staticfiles.finders.AppDirectoriesFinder'。 4. 静态文件未正确配置:在 urls.py 文件中,确保你正确地配置了静态文件的 URL。例如,如果你的静态文件位于 my_project/static/ 中,则应该添加以下代码:`from django.conf.urls.static import static from django.conf import settings urlpatterns = [ # ... your URL patterns here ... ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)`。 如果以上方法都没有解决问题,你可以检查你的静态文件目录和文件是否存在,以及文件是否具有正确的权限。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值