大家好! 我需要对静态标记进行一些特定的更改。这是我工作几小时的结果,但我得不到我需要的东西。以下代码只有在我删除" django.contrib.staticfiles"来自INSTALLED_APPS。但这不是正确的方法,因为我关闭了这个应用程序的整个功能。 请帮我解决这个问题。我如何具体说明" staticfiles"的优先级。
settings.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
# 'django.contrib.staticfiles',
)
的myapp / templatetags / staticfiles.py:
import re
from django import template
from django.contrib.staticfiles.templatetags.staticfiles import StaticFilesNode
from myapp.settings.local import STATIC_VERSION
class CustomStaticFilesNode(StaticFilesNode):
@classmethod
def handle_token(cls, parser, token):
path = versioning_js_and_css(
token.split_contents()[1],
get_actual_version()
)
node_obj = super().handle_token(parser, token)
node_obj.path = parser.compile_filter(path)
return node_obj
def get_actual_version():
return STATIC_VERSION
def versioning_js_and_css(path, version, tpl="'{path}?v={version}'"):
try:
cleaned_path = re.search("\'(.*?)\'", path).group(1)
if cleaned_path.endswith('.css') or cleaned_path.endswith('.js'):
path = tpl.format(path=cleaned_path, version=version)
except AttributeError:
pass
return path
def do_static_custom(parser, token):
return CustomStaticFilesNode.handle_token(parser, token)
register = template.Library()
register.tag('static', do_static_custom)
提前致谢!