I want to keep URL's prefix with app names in the main urls.py file. This way I will avoid collision of endpoints in other apps under the same project.
# project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/accounts/', include('account.urls')),
url(r'^api/v1/users/', include('users.urls')),
url(r'^api/v1/transactions/', include('transactions.urls')),
url(r'^auth-token-auth', views.obtain_auth_token),
]
For instance, I am including accounts.urls
with following content:
urlpatterns = [
url(r'^', AccountListView.as_view()),
url(r'^(?P<pk>[0-9]+)/$', AccountDetailView.as_view()),
]
By doing this way /api/v1/accounts/1
pattern does not match AccountDetailView
. It always returns the first view AccountListView
The most interesting thing is when I change urlpatterns
definition in the way provided below. All endpoints works as expected.
# project/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/v1/', include('account.urls')),
url(r'^api/v1/', include('users.urls')),
url(r'^api/v1/', include('transactions.urls')),
url(r'^auth-token-auth', views.obtain_auth_token),
]
# account/urls.py
urlpatterns = [
url(r'^accounts/$', AccountListView.as_view()),
url(r'^accounts/(?P<pk>[0-9]+)/$', AccountDetailView.as_view()),
]
The question is how to solve this issue by keeping app names api/v1/[app_name]
in main urls.py. Maybe you can suggest other patterns (best practice) for URL mapping.