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.
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