Use "basename" parameter while registering router. router.register(r'courses', CourseViewSet, basename="courses")
And then reverse using this basename and operation like "courses-list". On Tue, 26 Mar, 2024, 17:03 Filbert, <[email protected]> wrote: > *sigh* same error: > django.urls.exceptions.NoReverseMatch: Reverse for* 'courses-create' *not > found. 'courses-create' is not a valid view function or pattern name. > > On Tuesday, March 26, 2024 at 7:28:45 AM UTC-4 Muhammad Juwaini Abdul > Rahman wrote: > >> It's not reverse('courses') alone. Probably reverse('courses-create') or >> something like that. >> >> On Tue, 26 Mar 2024 at 18:55, Filbert <[email protected]> wrote: >> >>> Consider this what seems to be a simple Django/DRF API which works: >>> >>> class CourseViewSet(viewsets.ModelViewSet): >>> queryset = Course.objects.all() >>> serializer_class = CourseSerializer >>> >>> router = DefaultRouter() >>> router.register(r'courses', CourseViewSet) >>> >>> urlpatterns = [ >>> path('', include(router.urls)), >>> ] >>> >>> POST to: *http://localhost:9000/courses/ >>> <http://localhost:9000/courses/>* works from cURL >>> >>> But this code: >>> class CourseAPITest(TestCase): >>> def setUp(self): >>> self.client = APIClient() >>> self.course_data = {'description': 'course number 1', 'name': 'intro >>> to something'} >>> self.response = self.client.post(reverse('courses'), >>> self.course_data, >>> format='json') >>> >>> Gives me the error: >>> >>> *django.urls.exceptions.NoReverseMatch: Reverse for 'courses' not found. >>> 'courses' is not a valid view function or pattern name.* >>> >>> -- >>> 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 [email protected]. >>> To view this discussion on the web visit >>> https://groups.google.com/d/msgid/django-users/21615aab-a01a-4a1c-bc79-ed88908cafbbn%40googlegroups.com >>> <https://groups.google.com/d/msgid/django-users/21615aab-a01a-4a1c-bc79-ed88908cafbbn%40googlegroups.com?utm_medium=email&utm_source=footer> >>> . >>> >> -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/django-users/6360925a-32c8-41c5-966e-d71aa4161489n%40googlegroups.com > <https://groups.google.com/d/msgid/django-users/6360925a-32c8-41c5-966e-d71aa4161489n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOLgeEFVyE79A0wUphpBqXMRutD5x57%3DW7bMV0sN-qq6trD2MA%40mail.gmail.com.

