Write urls without regex in DRF

129 views
Skip to first unread message

Rounak Jain

unread,
May 22, 2019, 11:20:20 AM5/22/19
to django...@googlegroups.com

I am using DRF Viewsets to auto-generate URLs for different views. Is it possible to write the code below without using regex?
Thanks

from .views import TaskViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'', TaskViewSet, basename='task')
urlpatterns = router.urls




Onasanya Tunde

unread,
May 22, 2019, 12:41:41 PM5/22/19
to Django users
Use Django2.X

Rounak Jain

unread,
May 22, 2019, 10:22:00 PM5/22/19
to django...@googlegroups.com
Thanks for the reply. I understand how to use them in urls.py when creating normal routes but here in case of drf, it would help if you could show me how to do it

On Wed, May 22, 2019 at 10:11 PM Onasanya Tunde <onasany...@gmail.com> wrote:
Use Django2.X

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/67314c66-6d50-4390-80de-67d8151fe9a0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kevin Jay

unread,
May 22, 2019, 10:26:54 PM5/22/19
to django...@googlegroups.com
kevin@kjay,net

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Rounak Jain

unread,
May 23, 2019, 12:59:17 AM5/23/19
to Django users

I cannot see any reply 

On Thursday, May 23, 2019 at 7:56:54 AM UTC+5:30, Kevin Jay wrote:
kevin@kjay,net

On Wed, May 22, 2019 at 10:19 AM Rounak Jain <rounak...@gmail.com> wrote:

I am using DRF Viewsets to auto-generate URLs for different views. Is it possible to write the code below without using regex?
Thanks

from .views import TaskViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'', TaskViewSet, basename='task')
urlpatterns = router.urls




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

nm

unread,
May 23, 2019, 10:59:51 AM5/23/19
to Django users
Yes, it's possible to write this code without using regexp, and it should work the same.

If you have a look at the source code of the routers, there's a method `get_urls` in SimpleRouter (you can find it here https://github.com/encode/django-rest-framework/blob/master/rest_framework/routers.py), which, among others, does:

regex = route.url.format(
prefix=prefix,
lookup=lookup,
trailing_slash=self.trailing_slash
)

where `route.url` is something like
url=r'^{prefix}/{lookup}{trailing_slash}$',

and `prefix` is the first argument you pass to `router.register` - in your case, an empty string:
def register(self, prefix, viewset, basename=None, base_name=None)

It does not seem to matter whether you put `prefix=r''` or `prefix=''`, the ultimate url regex looks the same.
Disclaimer: I assume you're using the latest version of DRF (3.9.x); I haven checked how the code looks in older versions.
Reply all
Reply to author
Forward
0 new messages