13

I have a urls.py that looks like this:

router = SimpleRouter()
router.register(r'meetings', MeetingViewSet, 'meetings-list')

urlpatterns = patterns('clubs.views',
    url(r'^(?P<pk>\d+)/', include(router.urls)),
    url(r'^amazon/$', AmazonView.as_view(), name="amazon"),)

I want to reference the 'meetings-list' url using reverse, as in:

url = reverse('meetings-list')

but when I try this I get NoReverseMatch: Reverse for 'MeetingViewSet' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Is there a way to do this using Django Rest Framework?

2 Answers 2

21

When registering views with the router, you can pass the base_name in as the third argument. This base name is used to generate the individual url names, which are generated as [base_name]-list and [base_name]-detail.

In your case, you are registering your viewset as

router.register(r'meetings', MeetingViewSet, 'meetings-list')

So the base_name is meetings-list, and the view names are meetings-list-list and meetings-list-detail. It sounds like you are looking for meetings-list and meetings-detail, which would require a base_name of meetings.

router.register(r'meetings', MeetingViewSet, 'meetings')

You are also using the now-deprecated patterns syntax for defining urls, but you are not actually using the right url calls that work with it. I would recommend just replacing the patterns and wrapping your list of urls with a standard Python list/tuple ([] or ()).

This should fix your issue, and the call to reverse should resolve for you.

Sign up to request clarification or add additional context in comments.

Comments

3

I think this looks much better and cleaner for you:

router_urls = patterns(
    '',
    url(r'^meetings/$', MeetingViewSet.as_view(), 'meetings-list'),
)

urlpatterns = patterns(
    '',
    url(r'^(?P<pk>\d+)/', include(router_urls, namespace='router')),
)

Then, you wil do reverse('router:meetings-list', args=(pk, ))

I supposed that MeetingViewSet is a CBV

1 Comment

Just a heads up, Django REST Framework doesn't conform to the standard CBVs and this will trigger an error because you need to pass a dictionary into .as_view

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.