Django:动态添加应用程序作为插件,自动构建网址和其他设置

Kha*_*lla 5 python django web

我在Django中有以下文件夹结构:

./project_root
    ./app
       ./fixtures/
       ./static/
       ./templates/
       ./blog/
       ./settings.py
       ./urls.py
       ./views.py
       ./manage.py
       ./__init__.py 
    ./plugin
       ./code_editor
           ./static
           ./templates
           ./urls.py
           ./views.py
           ./__init__.py 
       ./code_viewer
           ./static
           ./templates
           ./urls.py
           ./views.py
           ./__init__.py 
Run Code Online (Sandbox Code Playgroud)

那么,如何通过查找基于INSTALLED_APPS的其他urls.py来使root urls.py自动建立url列表?我更改settings.py以动态构建INSTALLED_APPS,TEMPLATES_DIR,STATICFILES_DIRS.(这意味着我不知道将在不同的服务器中安装多少个插件.它应该在运行时动态检查并添加它.)on:

python manage.py runserver
Run Code Online (Sandbox Code Playgroud)

这是添加INSTALLED_APPS,TEMPATES_DIR,STATICFILES_DIR的代码.

PLUGINS_DIR = '/path_to/plugins/'

for item in os.listdir(PLUGINS_DIR):
    if os.path.isdir(os.path.join(PLUGINS_DIR, item)):
        plugin_name = 'app.plugins.%s' % item

    if plugin_name not in INSTALLED_APPS:
        INSTALLED_APPS = INSTALLED_APPS+(plugin_name,)

    template_dir = os.path.join(PLUGINS_DIR, '%s/templates/' % item)

    if os.path.isdir(template_dir):
        if template_dir not in TEMPLATE_DIRS:
            TEMPLATE_DIRS = TEMPLATE_DIRS+(template_dir,)

    static_files_dir = os.path.join(PLUGINS_DIR, '%s/static/' % item)

    if os.path.isdir(static_files_dir):
        if static_files_dir not in STATICFILES_DIRS:
            STATICFILES_DIRS = STATICFILES_DIRS + (static_files_dir,)
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.先感谢您.

解:

编辑:所以我做的如下:

我包括两个这样的模块:

from django.conf import settings
    from django.utils.importlib import import_module
Run Code Online (Sandbox Code Playgroud)

然后在root urls.py中添加以下代码:

def prettify(app):
    return app.rsplit('.', 1)[1]


for app in INSTALLED_APPS:

    try:
        _module = import_module('%s.urls' % app)
    except:
        pass
    else:
        if 'eats.plugins' in app:
            urlpatterns += patterns('',
                                    url(r'^plugins/%s/' % prettify(app), include('%s.urls' % app))
                                    )
Run Code Online (Sandbox Code Playgroud)

非常感谢@Yuka.谢谢.谢谢.谢谢.谢谢.... 你让我的一天.

Yuk*_*uka 5

你想要的是这样的:

from django.utils.importlib import import_module
for app in settings.INSTALLED_APPS:

    try:
        mod = import_module('%s.urls' % app)
        # possibly cleanup the after the imported module?
        #  might fuss up the `include(...)` or leave a polluted namespace
    except:
        # cleanup after module import if fails,
        #  maybe you can let the `include(...)` report failures
        pass
    else:
        urlpatterns += patterns('',
            url(r'^%s/' % slugify(app), include('%s.urls' % app)
        )
Run Code Online (Sandbox Code Playgroud)

你还想slugify从django模板或者utils中窃取并实现你自己的模板(我现在还不确定它在哪里?)并略微修改它以去除你不想要的任何"点"和其他无用的命名空间在你的'url'中你可能不希望你的网址看起来像这样:'example.com/plugin.some_plugin_appname/'但是喜欢example.com/nice_looking_appname/

你甚至可能不希望它自动命名,而是在你的插件自己的'设置'模块中做了一个可配置的"设置"或类似的东西:

# plugin settings conf
url_namespace = 'my_nice_plugin_url/'

# root urls.py:
url(r'^%s/' % mod.url_namespace, include(...))
# or:
url(r'^%s/' % app.settings.url_namespace, inc..
Run Code Online (Sandbox Code Playgroud)

你可能会得到它的要点.

亲切的问候,