Solution when Not Found appears when hitting the Django REST Framework API from the outside

problem

When I tried to display specific information by hitting the API of Django REST Framework (DRF) from Nuxt.js, Not Found appeared in the log on the Django side.

First of all, I thought that it was a problem on the Django side, so I checked it once on the DRF console, but the data exists and can be viewed. It's a simple matter, but I made an inadvertent mistake, so I'll leave it as an article just in case.

Solution

If Not Found occurs from the above prerequisites, you have made a mistake in setting the entry point.

In the first place, it is a premise, but in Django's routing, if a url other than the one described in urls.py is typed, it will be unconditionally Not Found if 404 error handling is not done. Therefore, there is a high possibility that the description of the entry point called on the Nuxt.js side is incorrect.

For example

async asyncData ({ $axios, params }) {
    try {
      const user = await $axios.$get('user')
      return { user }
    } catch (e) {
      return { user: [] }
    }

If you were trying to get the data in such a way, in urls.py of the project on the Django side

urls.py


urlspatterns = [
      path('api/',include('sample.urls')),
]

And write

sample/urls.py


urlspatterns = [
      path('user/',UserListView.as_view()),
]

You have to write in a format like this. This is because the data of ʻuser cannot be obtained unless it is GET from an address such as ʻapi / user in the same way as writing DRF. So you have to write it on the Nuxt.js side according to the routing on the Django side.

It's easy, but if you separate the front end and the back end and hit the API server from the outside, it's easy to overlook it. At least this time I was a little addicted to it. I'm glad that I got a better understanding of routing than before in this matter.

I hope this article helps someone.

Recommended Posts

Solution when Not Found appears when hitting the Django REST Framework API from the outside
Create a REST API to operate dynamodb with the Django REST Framework
Understand the benefits of the Django Rest Framework
Miscellaneous notes about the Django REST framework
Try hitting the Spotify API in Django.
How to automatically generate API document with Django REST framework & POST from document screen
I tried hitting the Qiita API from go
Spider not found appears when crawling with Scrapy
When I deployed the Django app to Heroku, I got Module Not Found: <project-name> .wsgi.
Solution when Linux does not recognize the built-in camera
Create a Todo app with the Django REST framework
When you want to filter with Django REST framework
[Django Rest Framework] Customize the filter function using Django-Filter
Creating an API that returns negative-positive inference results using BERT in the Django REST framework
Django REST framework basics
Django Rest Framework Tips
The first API to make with python Djnago REST framework
How to reset password via API using Django rest framework