Django DRF: Default URL always gets trigered first instead of route.url
<p>I have a very simple Django Rest Framework application, my <code>urls.py</code> looks like the following</p>

<pre><code>router = routers.DefaultRouter()
router.register(r'activity-list', activities.views.ArticleViewSet)

urlpatterns = [

    url(r'^admin/', admin.site.urls),

    url(r'^api/', include(router.urls, namespace='api')),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# Base/default URL, when all else fails
urlpatterns += [
    url(r'', TemplateView.as_view(template_name='index.html')),
]
</code></pre>

<p>When I run the url <code>http://127.0.0.1:8000/api/activity-list</code>, the route that comes from the DRF router never gets reached. The server always seems to give precedence to the default URL, and <code>index.html</code> is always rendered.</p>

<p>However, if I change the Default URL to:</p>

<pre><code># Base/default URL, when all else fails
urlpatterns += [
    url(r'BLABLABLA', TemplateView.as_view(template_name='index.html')),
]
</code></pre>

<p>Only then are the DRF routes reachable. This is what makes me think it's a precedence issue. The DRF routes are working, they just always get beaten by the default URL.</p>

<p>I've tried variants suggested in the <a href="http://www.django-rest-framework.org/api-guide/routers/#using-include-with-routershttp://" rel="nofollow">DRF docs</a>, without luck, such as :</p>

<p>Moreover, <code>/admin</code> works fine, which is why I suspect that it has to do with the DRF router in particular.</p>

<p>But the same thing keeps happening. How can I make my DRF routes reachable while maintaining a default route for the server to land on?</p>

<p><strong>Update:</strong> So I've found that adding a trailing slash to the api URL causes it to work. This has led me to these two (<a href="http://stackoverflow.com/questions/5948659/when-should-i-use-a-trailing-slash-in-my-url">1</a> and <a href="http://stackoverflow.com/questions/6545741/django-catch-all-url-without-breaking-append-slash">2</a>) questions.</p>

<p>You will have to learn <code>url dispatcher</code> in django. Your default url's and DRF route's url's <code>pattern</code> is same, and default url is on top of the <code>urlpatterns</code> thus that url is getting triggered.</p>

<p><a href="https://docs.djangoproject.com/en/1.10/topics/http/urls/#how-django-processes-a-request" rel="nofollow">How Django processes a request</a></p>

python django routes django-rest-framework