自定义runserver命令在静态文件上获取404

时间:2015-12-08 19:52:25

标签: python django

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

我在

创建自定义运行(runserver)命令 应用程序/同治/命令/ 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 

stataic load ok

[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

1 个答案:

答案 0 :(得分:4)

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