Is it possible to use the url template tag with DefaultRouter?

1,543 views
Skip to first unread message

fle...@fletchowns.net

unread,
Sep 24, 2013, 5:28:09 PM9/24/13
to django-res...@googlegroups.com
Let's say I've got the following in my urls.py:

from rest_framework import routers
from myproj import views

router = routers.DefaultRouter()
router.register(r'artist', views.ArtistViewSet)

If I want to use this API endpoint in one of my templates, I've just been hard coding it:

<form action="/myproj/artist/"></form>

Is it possible to use the url template tag to retrieve this url? Something like {% url 'myproj:artist' %} maybe? (I tried that but got the NoReverseMatch)

Thanks!

Tom Christie

unread,
Sep 25, 2013, 6:29:50 AM9/25/13
to django-res...@googlegroups.com
Yup absolutely.  Check out the view names in the documentation (last column in each table)


It'd be nice to provide some more informative debugging information about which routes are registered etc. Perhaps that'll happen at some point.

Hope that helps...

  Tom

fle...@fletchowns.net

unread,
Sep 27, 2013, 6:56:51 PM9/27/13
to django-res...@googlegroups.com
router.register(r'artist', views.ArtistViewSet, base_name='artist')

{% url 'myproj:artist-list' %}

Works great, thank you!!

fle...@fletchowns.net

unread,
Dec 6, 2013, 3:30:48 PM12/6/13
to django-res...@googlegroups.com
Is it also possible to use {% url 'whatever' %} to get the url of a Function Based View? I can't seem to get it to work.

Nasaruddin nasa

unread,
Dec 7, 2013, 3:02:44 AM12/7/13
to django-res...@googlegroups.com
you need to define the url on urls.py

url('/your/url', 'your.views', name="url_name");

then in templates {% url 'url_name' %}


--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages