0

I have multiple apps that share almost the same URL patterns, defining almost the same views. Currently i have a urls.py for each app to serve its routing. What i want to do is to group the similar patterns into a single shared_urls.py file then use it in those apps.

To make it easy to understand, suppose (just an example here) i have a blog app and an archive app. They both define a pattern and view for /post, /comment and /user. So instead of each of them having its own urls.py defining the same pattern, i want to define these patterns in one place then use it in each of the apps, while loading the correct app view.

Current Vs Wishing

Current

project urls.py

url(r'^blog/', include('blog.urls')),
url(r'^archive/', include('archive.urls')),

blog urls.py

url(r'^post/', views.post),
url(r'^comment/', views.comment),
url(r'^user/', views.user),

archive urls.py

url(r'^post/', views.post),
url(r'^comment/', views.comment),

As you see the two apps share almost the same patterns but each has its own implemented view.

Wishing

project urls.py

url(r'^blog/', include('blog.urls')),
url(r'^archive/', include('archive.urls')),

shared_urls.py

#How to bind with the correct app's view!
url(r'^post/', views.post),
url(r'^comment/', views.comment),

blog urls.py

url(r'^user/', views.user),
url(r'', include(shared_urls)),

archive urls.py

url(r'', include(shared_urls)),
amigcamel
  • 1,879
  • 1
  • 22
  • 36

1 Answers1

0

This is a logic (controller/view) matter, not a url (routing) matter. Sharing URLs would be a bad practice. They should be clearly separately defined since they provide different actions. The fact that you want to use the same logic/content behind two different url must be handled in your views.

Robin
  • 1,531
  • 1
  • 15
  • 35