Anuj
Anuj

Reputation: 563

Import urls in urls.py based on INSTALLED_APPS Django

I havesocial_networks app which is controlled from settings.py i.e whether to import it in main application or not.

The way I achieved is:

`from  social_networks import fb_settings`

`fb_settings.modify(globals())`

If I want to stop the support of app and it's functionality I have to manually comment out the code during which modify will not be called.

In my fb_settings.py which is a part of social_networks app, I update the INSTALLED_APPS to reflect the new app in modify function .

I have main urls.py I have include('social_networks.urls') but I want to make it more modular to not include if the app is not installed. Currently I am checking the settings.INSTALLED_APPS variable to check if the app is present and based on that include('social_networks.urls').

Is this the proper way to do things in django or I am missing something already available form django that can help me?

Upvotes: 3

Views: 1205

Answers (1)

Aamir Rind
Aamir Rind

Reputation: 39659

I think this is the good way (which you have already mentioned):

# define necessary urls of your app which should always be present
urlpatterns = patterns('',
    url(r'^$', 'myapp.views.home', name='home'),
)

# then add urls of external apps if the app is present in INSTALLED_APPS
if 'social_networks' in settings.INSTALLED_APPS:
    urlpatterns += patterns('',
        url(r'^social/', include('social_networks.urls')),
    )

Upvotes: 4

Related Questions