Ich entwickle eine Django-Anwendung, die dynamische Module (zur Laufzeit) laden muss. Jetzt bin ich in der Lage, "Plugins" (vom Client-Browser zum Server) hochzuladen und das Plugin-Modell in der Datenbank usw. zu registrieren. Aber ich brauche einen Weg, um die URL-Muster für jedes Plugin zu handhaben. Momentan habe ich eine Funktion im "Kern" der Webapp geschrieben, die ein Modell registriert und (theoretisch) die URL-Muster des hochgeladenen Plugins zur webapp urls.py hinzufügt. Diese Funktion ist:Modifizieren von URL-Mustern zur Laufzeit in Django
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')))
Plugins zu „media/tmp“ in tgz-Format hochgeladen werden und dann extrahiert, um „Medien/plugins /“ wo ist der Codename des Plug-Ins, und die Benutzer hochgeladen Plugins verwaltet durch " project.plugins ".
Alle Plugins Logik funktioniert gut, aber wenn ich versuche, die hochgeladene plugin urls.py Datei in die Webapp (project.plugins.urls) aufzunehmen, hat es keine Wirkung. Ich habe den Wert von "project.plugins.urls.urlpatterns" gedruckt und es wird nicht nach "urlpatterns + = pat ...." geändert.
Gibt es eine Möglichkeit zu tun, was ich brauche?
Mit freundlichen Grüßen