如何在Django中使用manage.py列出所有已安装的应用程序?

时间:2014-02-05 01:24:01

标签: django

一些manage.py命令将Django应用程序作为参数。有时我想使用这些命令,但不记得应用程序的名称。有没有办法让manage.py提供这样的列表?

1 个答案:

答案 0 :(得分:5)

尚未准备好,但你可以管道:

$ echo 'import settings; settings.INSTALLED_APPS' | ./manage.py shell
...
>>> ('django.contrib.auth', 'django.contrib.contenttypes', 
     'django.contrib.sessions', 'django.contrib.sites'...]

或写一个小custom command

import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
    def handle(self, *args, **options):
        print settings.INSTALLED_APPS

或以更通用的方式:

import settings
from django.core.management.base import BaseCommand
class Command(BaseCommand):
    def handle(self, *args, **options):
        print vars(settings)[args[0]]

$ ./manage.py get_settings INSTALLED_APPS
('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 
 'django.contrib.sites', ...]
$ ./manage.py get_settings TIME_ZONE
America/Chicago