• django 静态文件


    django 1.8版本以上  

    django 静态文件配置。

    小作之前, 一直觉得django的静态文件配置非常的麻烦。

    1. 要设置url(r'^static/(?P<path>.*)&', django.views.static.serve, {'document_root': settings.STATIC_ROOT}

    2. settings.py 文件中需要添加变量STATIC_ROOT

    3.html中引入静态文件还需要href="{% static 'xxxx/xxx/xxxx' %}"

    这样的步骤非常的麻烦,非常容易出错。

    后来小作发现。

    django是默认指定了静态文件的查找路径。我们可以直接使用默认的就很方便快捷了。

    MySite 

      MySite

        __init__.py

        settings.py

        urls.py

        wsgi.py

      MyApp1

        __init__.py

        models.py

        views.py

        urls.py

        migrations

          __init__.py

        static

          MyApp1

            js

              vue.min.js

            css

              htt.css

            images

              xxx.png

        templates

          aaa.html

          bbb.html

      MyApp2

        __init__.py

        models.py

        views.py

        urls.py

        migrations

          __init__.py

        static

          MyApp2

            js

              vue.min.js

            css

              htt.css

            images

              xxx.png

        templates

          ccc.html

          ddd.html

    现在需要在ccc.html中引入静态文件vue.min.js, 在MyApp文件下创建一个static文件夹(django默认会到app或者项目主文件去中查找static文件),然后在static文件下创建名为MyApp1的文件夹(通过app的名字可以区分不同app下面的static文件夹,保持路径唯一),然后找MyAPP1下面的文件。

    ## ccc.html
    
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
        {% load staticfiles %}
        <script src="{% static 'MyApp2/js/vue.min.js' %}"></script>
    </head>
    
    
    ......

    Django服务器,将默认在每个App下面的static文件夹下面找MyApp2/js/vue.min.js, 在APP(MyApp1)下面的static文件下没匹配到MyApp2, 那么就跳到下一个APP(MyApp2)下面的static文件下午匹配MyApp2, 逐层匹配下面,知道找到静态文件并引用。

        

  • 相关阅读:
    Saltstack_使用指南09_远程执行-编写执行模块
    Saltstack_使用指南08_远程执行-返回程序
    Saltstack_使用指南07_远程执行-执行模块
    Saltstack_使用指南06_远程执行-指定目标
    CentOS7 Docker私有仓库搭建及删除镜像 【转】
    Python Docker 查看私有仓库镜像【转】
    Saltstack_使用指南05_数据系统-Pillar
    Saltstack_使用指南04_数据系统-Grains
    Saltstack_使用指南03_配置管理
    Saltstack_使用指南02_远程执行-验证
  • 原文地址:https://www.cnblogs.com/haoshine/p/5834022.html
Copyright © 2020-2023  润新知