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, 逐层匹配下面,知道找到静态文件并引用。

        

  • 相关阅读:
    大爽Python入门教程 45 实践使用
    大爽pyqt5笔记&教程 四 布局 Layout 对其
    大爽Python入门教程 47 答案
    大爽Python入门教程 46 习题
    大爽Python入门教程 43 函数传参 形参、实参 default、*args、**kwargs
    大爽Python入门教程 41 初识函数Function
    大爽Python入门教程 56 实践练习 功能添加
    检测密码是否能过强度检测(正则表达式检测)
    常用加密算法汇总一下
    [转]技术人员,你拿什么拯救你的生活温水煮青蛙
  • 原文地址:https://www.cnblogs.com/haoshine/p/5834022.html
Copyright © 2011-2022 走看看