Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django - Static Files

Back


Cheapsheet

Development Deployment Description
STATIC_URL   Static files url
STATICFILES_DIRS   Global Static File Physical location
  STATIC_ROOT Physical location for collectstatic

Static Files


Configuring Static Files During Development

settings.py


Templates

<!-- add at the top of html codes -->
<!-- load static -->

<!-- using static tag to referencing static files -->
<img src="static 'my_app/example.jpg'" alt="My image"> <!-- without {} % signs due to github deploy --->
<link rel="stylesheet" type="text/css" href="static '/Workout/carousel.css' " />

Static Files Folder


Alternative

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Configuring Static Files During Deployment(IIS)


settings.py


collectstatic


创建 web.config 文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
    <!-- 覆盖FastCGI handler,让IIS访问静态文件 -->
      <handlers>
        <clear/>
        <add name="StaticFile"
             path="*"
             verb="*"
             modules="StaticFileModule"
             resourceType="File"
             requireAccess="Read" />
      </handlers>
    </system.webServer>
</configuration>

代码说明:

  • 如果没有该代码,当浏览器打开 static_root 地址时会返回 url 错误的信息;当存在该代码时,会返回 404 的服务器代码,即将 static_root 的访问的路由错误变为服务器的权限错误。

设置 IIS 虚拟目录

  1. 打开 IIS
  2. 点选网站,右键”添加虚拟目录”

图片

  1. 设置参数。
    • 别名:站点显示的虚拟文件目录名称。
    • 物理路径:指定\/static_collected\/文件夹。

图片3

图片4


TOP