zoukankan      html  css  js  c++  java
  • 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, 逐层匹配下面,知道找到静态文件并引用。

        

  • 相关阅读:
    JS实战 · 表单验证
    JS实战 · 仿css样式选择器
    JS实战 ·  收缩菜单表单布局
    cookie自动登录的实现
    redis 3.2.5单机版安装、使用、systemctl管理Redis启动、停止、开机启动
    yum问题解决
    配置yum镜像源
    shell笔记
    CCIE总结:路由器、交换机
    云主机如何挂在磁盘
  • 原文地址:https://www.cnblogs.com/haoshine/p/5834022.html
Copyright © 2011-2022 走看看