我正在开发一个需要加载动态模块(运行时)的Django应用程序。现在我可以上传(从客户端浏览器到服务器)“插件”,并在数据库中注册插件模型等,但我需要一种方法来处理每个插件的urlpatterns。目前,我已经在注册模型的webapp的“核心”中编写了一个函数,并且(理论上)通过包含它将上传的插件的urlpatterns添加到webapp urls.py中。这个函数是:在Django运行时修改urlpatterns
def register_plugin_model(model,codename):
# Standard syncdb expects models to be in reliable locations,
# so dynamic models need to bypass django.core.management.syncdb.
# On the plus side, this allows individual models to be installed
# without installing the entire project structure.
# On the other hand, this means that things like relationships and
# indexes will have to be handled manually.
# This installs only the basic table definition.
if model is not None:
style = color.no_style()
cursor = connection.cursor()
tables = connection.introspection.table_names()
seen_models = connection.introspection.installed_models(tables)
statements,trsh = connection.creation.sql_create_model(model, style, seen_models)
for sql in statements:
cursor.execute(sql)
# add urlpatterns
from django.conf.urls.defaults import patterns, url,include
from project.plugins.urls import urlpatterns
urlpatterns += patterns(url(r'^' + codename + '/' , include ('media.plugins.' + codename + '.urls')))
插件上传到在TGZ格式“媒体的/ tmp”,然后萃取到“介质/插件/”,其中是插件的代号,并且用户上传的插件由管理“ project.plugins”。
所有插件逻辑工作正常,但是当我尝试将上传的插件urls.py文件包含到webapp(project.plugins.urls)时,它不起作用。我已经打印了“project.plugins.urls.urlpatterns”的值,并且在“urlpatterns + = pat ....”之后没有修改。
有什么办法可以做我需要的吗?
问候