• Django项目DEBUG=False时配置静态文件


    Django项目在发布时,一般会设置:DEBUG = False,但这时系统不会自动给配置路由,需要我们手工配置,那么配置的方法如何呢?其实很简单,关键有两点:

    关键点一:

    在项目的urls.py文件中,添加:

    from django.conf.urls import url, static
    from django.contrib import admin
    from django.urls import path
    
    from [项目名] import views, settings
    
    urlpatterns = [
        url(r'^static/(?P<path>.*)$', static.serve,
            {'document_root': settings.STATIC_ROOT}, name='static'),
    ]

    关键点在于url的路由转发,将路径带static的请求,全部按settings.STATIC_ROOT路径来,这里的settings.STATIC_ROOT也可以换成其他变量。

    关键点二:

    根据以上的引用,在settings.py文件中配置STATIC_ROOT:写法太多了,比如(当然以下语句前提是运行了命令:python manage.py collectstatic,将静态文件收集到了collect_static文件夹):

    STATIC_ROOT = os.path.join(BASE_DIR, "collect_static")

    或者:

    STATIC_ROOT = os.path.join(BASE_DIR, "static")

    更或者:

    STATIC_ROOT = '/static/'

    跟甚至:

    STATIC_ROOT = 'static'

    以上均可,以上写法均可实现静态资源加载。其关键点在于首先在urls.py中进行规则配置,其二,根据urls配置情况,设置其引用的变量路径。

    html写法:

    {% load static %}
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <link rel="stylesheet" href="{% static 'css/abc.css' %}">
    </head>
    <body>
    <p class="red">dsfdsdfsdfs</p>
    </body>
    </html>

    文章出处:www.cnblogs.com/jizhong

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。否则保留追究法律责任的权利。

  • 相关阅读:
    AX 2012 Security Framework
    The new concept 'Model' in AX 2012
    How to debug the SSRS report in AX 2012
    Using The 'Report Data Provider' As The Data Source For AX 2012 SSRS Report
    Deploy SSRS Report In AX 2012
    AX 2012 SSRS Report Data Source Type
    《Taurus Database: How to be Fast, Available, and Frugal in the Cloud》阅读笔记
    图分析理论 大纲小结
    一文快速了解Posix IO 缓冲
    #转载备忘# Linux程序调试工具
  • 原文地址:https://www.cnblogs.com/jizhong/p/15131824.html
Copyright © 2020-2023  润新知