Modifing urlpatterns at runtime in Django Modifing urlpatterns at runtime in Django django django

Modifing urlpatterns at runtime in Django


The problem you're facing, is that urlpatterns defined in your projects url.py file, and the urlpatterns defined in your register_plugin file are different variables. They are local to the module. Imagine the following scenario:

#math.pypi = 3.14#some_nasty_module.pyfrom math import pipi = 'for_teh_luls'#your_module.pyfrom math import pipi * 2>>> 'for_teh_lulsfor_teh_luls'# wtf?

You're obviously not allowed to do this. What you're probably going to need to do, is ask the original urls.py to attempt to discover urls in your plugins folder.

# urls.pyurlpatterns += (...)def load_plugin_urls():    for module in get_plugin_modules():        try:            from module.urls import urlpatterns as u            urlpatterns += u        except:            pass

Unfortunately, the webserver will need to recycle the process for this code to run, so uploading of plugins will only come into effect once that happens.


The function that modifies urls.py and urls.py belong to the same module. I've solved it by adding an "empty pattern":

urlpatterns += patterns('',url(r'^' + codename + '/' , include ( 'media.plugins.' + codename + '.urls' )))

Now, it says:

BEFORE:<RegexURLPattern plugins ^$><RegexURLPattern plugin_new ^new/$><RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$>AFTER<RegexURLPattern plugins ^$><RegexURLPattern plugin_new ^new/$><RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$><RegexURLResolver media.plugins.sampleplugin.urls (None:None) ^sampleplugin/>

But has you said, it doesn't take effect instantaneously :/

I've restarted the application without deleting *.pyc files, but changes didn't taken effect. What's the problem?

PD: the plugin urls.py file contains:

from django.conf.urls.defaults import patterns, urlfrom .views import index_viewurlpatterns = patterns( '' , url(r'^.*' , index_view ) )

Thanks for your reply

Best regards