adding urls in API Root

1,746 views
Skip to first unread message

Miguel Fiandor Gutiérrez

unread,
Apr 9, 2014, 7:48:49 AM4/9/14
to django-res...@googlegroups.com
Hi,

I am registering urls in the API Root using DefaultRouter for my viewsets in views.py, but I also would like to include urls from a specific based APIView, and these last ones don't show up in my API Root.

This is my code:

urls.py

from django.conf.urls import url, patterns, include
from rest_framework import viewsets, routers
from rest_framework.generics import ListCreateAPIView
from tdcp_reports import views
from rest_framework.urlpatterns import format_suffix_patterns

# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()

router.register(r'aapps', views.PublicAdministrationPaginated)
router.register(r'indicators', views.IndicatorDescriptionPaginated)
router.register(r'local_indicators', views.LocalIndicatorPaginated)

urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    url(r'^province/', views.province_view, name='test-province'),   # <------------ THIS ONE DOESN'T SHOW UP
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

--------------
views.py

...
@api_view(['GET'])
def province_view(request):
    queryset = Province.objects.all()
    renderer_classes = [r.CSVRenderer] + api_settings.DEFAULT_RENDERER_CLASSES

    def get(self, request, format=None):
        """
        Return a list of all provinces.
        """
        return Response(queryset)
....

Reinout van Rees

unread,
Apr 9, 2014, 10:48:38 AM4/9/14
to django-res...@googlegroups.com
On 09-04-14 13:48, Miguel Fiandor Gutiérrez wrote:
> urlpatterns = patterns('',
> url(r'^', include(router.urls)),
> url(r'^province/', views.province_view, name='test-province'), #
> <------------ THIS ONE DOESN'T SHOW UP
> url(r'^api-auth/', include('rest_framework.urls',
> namespace='rest_framework')),
> )

Django matches from the top to the bottom. The first matching one wins.

r'^' means "any url that has a beginning". So... this one matches
absolutely everything :-)

You probably meant r'^$'. Anyway, moving it right to the bottom of the
list should already help.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

Miguel Fiandor Gutiérrez

unread,
Apr 9, 2014, 11:52:19 AM4/9/14
to django-res...@googlegroups.com, rei...@vanrees.org
I have tried your suggestions, but they didn't work.

  1. Moving the blank one to the end doesn't change anything
  2. adding the $ sign, so it looks like: r'^$' , raises a NoReverseMatch error for my first ViewSetrouter register => router.register(r'aapps', views.PublicAdministrationPaginated)

I've checked documentation and stackoverflow, and I found that both point that the url regex should be without the '$' sign¨:
Reply all
Reply to author
Forward
0 new messages