0

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.

1 Answer 1

1

Your second version of the URLs correctly terminates the account list pattern, but your first does not.

url(r'^$', AccountListView.as_view()),
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.