【问题标题】:Custom runserver command gets 404 on static files自定义 runserver 命令在静态文件上获取 404
【发布时间】:2015-12-08 19:52:24
【问题描述】:

我尝试在启动 django 服务器时将自定义 ascii 徽标添加到控制台输出中,问题是我的静态文件得到 404。我该怎么做:

我在

创建自定义运行(运行服务器)命令 应用程序/管理/命令/run.py
from __future__ import unicode_literals

from django.core.management.commands import runserver 

LOGO = """
    >>ASCII LOGO<<<
"""

class Command(runserver.Command):
    def inner_run(self, *args, **options):
        self.stdout.write(LOGO)
        super(Command, self).inner_run(self, *args, **options)

# Kept for backward compatibility
BaseRunserverCommand = Command

我跑

 python manage.py run

服务器启动,一切正常,然后我得到

[08/Dec/2015 19:46:02] "GET /monitor/board HTTP/1.1" 200 6911
Not Found: /static/js/plugins/masonry.pkgd.min.js
[08/Dec/2015 19:46:02] "GET /static/js/plugins/masonry.pkgd.min.js HTTP/1.1" 404 2803
Not Found: /static/js/custom/board.js
[08/Dec/2015 19:46:02] "GET /static/js/custom/board.js HTTP/1.1" 404 2767

但是当我运行它时

python manage.py runserver 

静态加载正常

[08/Dec/2015 19:49:25] "GET /monitor/board HTTP/1.1" 200 6911
[08/Dec/2015 19:49:25] "GET /static/js/custom/board.js HTTP/1.1" 200 147
[08/Dec/2015 19:49:25] "GET /static/js/plugins/masonry.pkgd.min.js HTTP/1.1" 200 28953

【问题讨论】:

    标签: python django


    【解决方案1】:

    staticfiles 应用程序使用自定义 runserver 命令在开发中提供静态文件。要保持这种行为,您需要改为将该命令子类化:

    from django.contrib.staticfiles.management.commands import runserver 
    
    LOGO = """
        >>ASCII LOGO<<<
    """
    
    class Command(runserver.Command):
        def inner_run(self, *args, **options):
            self.stdout.write(LOGO)
            super(Command, self).inner_run(self, *args, **options)
    
    # Kept for backward compatibility
    BaseRunserverCommand = Command
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-02
      • 2018-07-10
      • 2016-09-02
      • 2013-02-13
      • 2014-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多