Anton Koval'
Anton Koval'

Reputation: 5053

Extend urlpatterns dynamically

I have application app1. It has connected (through INSTALLED_APPS) app2; let's say that app2 is some kind of black-box for me. But I know that app2 in its urls has some number of i18n_patterns.
I need to include all urls from app2 to my app1. And include them at root position:

urlpatterns = ('',
    (r'', include("app1.urls")),
    (r'', include("app2.urls")),
)

because of i18n_patterns in app2.urls such include will raise
ImproperlyConfigured('Using i18n_patterns in an included URLconf is not allowed.')
source code here

Are there ways to append all the urlpatterns from app2.urls to my urlpatterns without knowing much about them?

Upvotes: 1

Views: 2353

Answers (1)

Paulo Bu
Paulo Bu

Reputation: 29794

For instance, you can from app2.urls import urlpatterns as urlpatterns2 and later in your urls.py at the end do this:

urlpatterns += urlpatterns2
# or maybe: 
# urlpatterns += urlpatterns2[1:] if you don't want to include the initial attribute

This behaves like a normal list concatenation and might work.

Note: This is a bit of hackery to overcome the include limitants. If there is a better way to do this I'll love to know.

Hope this helps!

Upvotes: 6

Related Questions