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

2024-05-19 01:40:48 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我在以下位置创建自定义运行(runserver)命令

应用程序/管理/命令/运行.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

Tags: runpyselfboardhttpgetjsplugins
1条回答
网友
1楼 · 发布于 2024-05-19 01:40:48

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

相关问题 更多 >

    热门问题