Django:动态添加应用程序作为插件,自动构建网址和其他设置 [英] Django: Dynamically add apps as plugin, building urls and other settings automatically

查看:135
本文介绍了Django:动态添加应用程序作为插件,自动构建网址和其他设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have following structure of my folders in 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 

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

So, how can I make root urls.py automatically build up the list of urls by looking for other urls.py based on the INSTALLED_APPS? I change settings.py in order to build INSTALLED_APPS, TEMPLATES_DIR, STATICFILES_DIRS dynamically. (It means i do not know how many plugins will be installed in different servers. It should dynamically check it on run time and add it.)on:

python manage.py runserver

此处是用于添加INSTALLED_APPS,TEMPATES_DIR,STATICFILES_DIR的代码

Here is code for adding 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,)

任何帮助将不胜感激。

Any help will be appreciated. Thank you in advance.


解决方案:



编辑:因此,我的工作如下:

SOLUTION:

So what i did are as following:

我包括两个这样的模块:

I include two modules like this:



from django.conf import settings
    from django.utils.importlib import import_module




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

And then in root urls.py I add following code:



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))
                                    )




非常感谢@Yuka。谢谢。谢谢。谢谢。谢谢。...
你让我开心。

Thank you a lot @Yuka. Thank you. Thank you. Thank you. Thank you.... You make my day.


推荐答案

您想要的是这样的

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)
        )

您会还想从django模板或实用程序中窃取并实现您自己的 slugify (我现在不确定它现在在哪里吗?),并稍加修改以删除任何点和您不希望在 URL中使用的其他无用的命名空间,例如,您可能不希望自己的URL看起来像这样: example.com/plugin.some_plugin_appname/,但类似于 example.com/nice_looking_appname /

You'll also want to steal and implement your own slugify from django template or utils (I'm not exactly sure where it lives these days?) and slightly modify it to strip out any 'dots' and other useless namespacing you don't want in your 'url' e.g. you might not want your urls looking like so: 'example.com/plugin.some_plugin_appname/' but like example.com/nice_looking_appname/

您甚至可能根本不希望它自动命名,而是在插件自己的设置模块中进行可配置的设置或类似的东西:

You might even not want it automagicly named after all, and instead made a configurable 'setting' in your plugins own 'settings' module or something like so:

# 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..

您可能会明白要点。

亲切的问候,

这篇关于Django:动态添加应用程序作为插件,自动构建网址和其他设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆