S
S
sttpforever2021-06-12 11:08:44
URL Handling
sttpforever, 2021-06-12 11:08:44

I am learning to write api in djangorestframework. The problem is with the viewsets.ModelViewSet class. How to write it correctly and make a router in the url?

I am learning to write api in django rest framework. In this case, you need to use the viewsets.ModelViewSet class. What should generate two endpoints: a POST request to api/v1/posts/ will create a new entry; PUT, PATCH or DELETE requests to api/v1/posts// will change or delete the existing entry. In the class itself, it seems that only two lines, as I understand it, need to be written. But I got confused with the url - what to enter in the head url, and what in the application url, I couldn’t figure it out here, no matter how I searched for information. Here is my code https://pastebin.com/VKA2pg8t . Please help me figure it out, because while it’s impossible to correctly register these two endpoints, out of desperation, I began to shove everything and everywhere in the hope that the extra would not hurt, and if I guess right, then I’ll start deleting the extra. Could someone please explain how it works.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TheMrWhite, 2021-06-17
@TheMrWhite

Try this:
head url - path('api/', include('appname.urls')),
application url - router.register('posts', PostViewSet)
path('v1/', include(router.urls))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question