Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call a Django Rest Framework URL using namespacing?

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?

like image 792
YPCrumble Avatar asked Dec 15 '14 16:12

YPCrumble


People also ask

What is namespace in Django URL?

URL namespaces allow you to uniquely reverse named URL patterns even if different applications use the same URL names. It's a good practice for third-party apps to always use namespaced URLs (as we did in the tutorial). Similarly, it also allows you to reverse URLs if multiple instances of an application are deployed.

What is URL routing in Django?

A route can be defined as a URL that displays a particular web page on the browser. For example, if we want to visit the Educative website, we would head over to https://www.educative.io. Django lets us route URLs however we want and with no framework limitations.

What is the correct command to run the Django REST server?

Setting up Django REST framework Run the command python -m venv django_env from inside your projects folder to create the virtual environment. Then, run source ./django_env/bin/activate to turn it on. Keep in mind that you'll need to reactivate your virtual environment in every new terminal session.

How do you use a router in DRF?

Routers are used with ViewSets in django rest framework to auto config the urls. Routers provides a simple, quick and consistent way of wiring ViewSet logic to a set of URLs. Router automatically maps the incoming request to proper viewset action based on the request method type(i.e GET, POST, etc).


1 Answers

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.

like image 182
Kevin Brown-Silva Avatar answered Sep 17 '22 20:09

Kevin Brown-Silva