Reputation: 1537
I am using Django
with Django Rest Framework
.
Django==2.0.2
djangorestframework==3.7.7
I am trying to route to a function view
.
My current setup looks like this:
- project
- project
- urls
- app
- urls
- views
In project/urls
, I reference the app's
urls like this:
url(r'^app_api/', include('app_api.urls', namespace='app_api'))
In app/urls
, I route to the views like this:
router.register(r'', views.app_api, base_name="app_api")
url(r'', include(router.urls)),
In app/views
I have defined a function called app_api
and decorated it with api_view
.
@api_view(['GET'])
def app_api(request):
If I run the server and curl http://localhost:8080/app_api/
for example, I get a response with status 200 and a empty JSON.
"GET /app_api/ HTTP/1.1" 200 2
{}
I do not understand why this is happening. If I use a class based viewset, it works, but not with a function view.
What is the correct way of routing to a function view when using Django Rest Framework
?
Thanks!
Upvotes: 5
Views: 5150
Reputation: 599470
I don't understand why you are using a router here. Those are for viewsets: the router generates all URL endpoints for the viewset. You have a single view; it makes no difference if it's a class or function, you need to declare an explicit endpoint for it just like in plain Django.
Upvotes: 3