python 在不涉及整个项目的情况下调用django collectstatic,灵感来自https://github.com/syntarsus/minimal-django

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 在不涉及整个项目的情况下调用django collectstatic,灵感来自https://github.com/syntarsus/minimal-django相关的知识,希望对你有一定的参考价值。

# coding: utf-8
# This is the original implementation before I got inspiration from minimal-django

import os
import sys
import string
import random
import importlib
from django.core.management import execute_from_command_line


settings_template = """# coding: utf-8

SECRET_KEY = 'A-random-secret-key!'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.staticfiles',
]

STATIC_URL = '{static_url}'
STATIC_ROOT = '{static_root}'
"""


if __name__ == '__main__':
    django_settings_module = sys.argv[1]

    settings = importlib.import_module(django_settings_module)

    pure_settings = settings_template.format(
        static_url=settings.STATIC_URL,
        static_root=settings.STATIC_ROOT,
    )

    rand_str = ''.join(random.choice(string.ascii_lowercase) for i in xrange(5))
    prefix = 'django_settings_{}'.format(rand_str)
    filename = prefix + '.py'

    with open(filename, 'w') as f:
        f.write(pure_settings)

    print 'write to tempfile: {}'.format(filename)

    os.environ.setdefault('DJANGO_SETTINGS_MODULE', prefix)
    execute_from_command_line(['manage.py', 'collectstatic', '--no-input', '-c'])

    os.unlink(filename)
# coding: utf-8
# Usage: python django_static_collector.py myapp.settings

import sys
import importlib

from django.conf import settings
from django.core.management import execute_from_command_line


def main():
    app_settings_module = sys.argv[1]
    app_settings = importlib.import_module(app_settings_module)

    settings.configure(
        DEBUG=True,
        SECRET_KEY='A-random-secret-key!',
        INSTALLED_APPS=[
            'django.contrib.admin',
            'django.contrib.contenttypes',
            'django.contrib.staticfiles',
        ],
        STATIC_URL=app_settings.STATIC_URL,
        STATIC_ROOT=app_settings.STATIC_ROOT,
    )

    execute_from_command_line(['manage.py', 'collectstatic', '--no-input', '-c'])


if __name__ == '__main__':
    main()

以上是关于python 在不涉及整个项目的情况下调用django collectstatic,灵感来自https://github.com/syntarsus/minimal-django的主要内容,如果未能解决你的问题,请参考以下文章

如何在不刷新整个页面的情况下重新加载组件?

Android 啥时候会在不破坏整个进程的情况下破坏活动?

如何在不重新加载整个页面的情况下刷新 div?

在不停止整个程序的情况下休眠 Lua 脚本?

在不重新加载整个表格视图的情况下更改 UITableView 的部分页眉/页脚标题

如何在不修改生产代码的情况下打破依赖关系?