我不确定为什么我的COUNTRY_DETAIL API不工作.我有另一个ListAPIView,它非常相似,工作正常.
以下是代码:
Urls.py:
path('api/country/<str:name>/', views.CountryDetailView.as_view(), name='country_detail'),
Views.py:
# get a specific country object by name
class CountryDetailView(generics.RetrieveAPIView):
permission_classes = (permissions.AllowAny,)
authentication_classes = (SessionAuthentication,)
serializer_class = CountrySerializer
lookup_field = 'name'
def get_queryset(self):
name = self.kwargs['name'].capitalize()
queryset = Country.objects.filter(name=name)
print(name)
print(queryset)
return queryset
Reaction组件:
export default function Country() {
const [countryDetail, setCountryDetail] = useState({});
const { country } = useParams();
useEffect(() => {
const fetchData = async () => {
try {
const response1 = await axios.get(
`http://localhost:8000/api/country/${country}/`
);
setCountryDetail(response1.data);
} catch (error) {
console.error(error);
} finally {
console.log(countryDetail);
}
};
fetchData();
}, [country]);
return (
<div id="country-container">
<header>{countryDetail.description}</header>
<main>
...
</main>
</div>
);
}
访问Reaction路由时:
<Route path="/world/:continent/:country" element={<.../>} />
非洲大陆参数为非洲,埃塞俄比亚为国家参数
http://localhost:3000/world/africa/ethiopia
UseEffect使用Country参数从
`http://localhost:8000/api/country/${country}/`
一切似乎都很正常,它获取了正确的数据,但响应似乎是空的……
以下是要求, 如您所见,print(Name)和print(Queryset)获得正确的国家名称和正确的对象,但仍然抛出404.
[05/May/2023 18:56:53] "OPTIONS /user HTTP/1.1" 200 0
[05/May/2023 18:56:53] "OPTIONS /user HTTP/1.1" 200 0
[05/May/2023 18:56:53] "GET /user HTTP/1.1" 200 54
[05/May/2023 18:56:53] "GET /user HTTP/1.1" 200 54
[05/May/2023 18:56:53] "OPTIONS /api/country/ethiopia/ HTTP/1.1" 200 0
[05/May/2023 18:56:53] "OPTIONS /api/country/ethiopia/ HTTP/1.1" 200 0
Ethiopia
Ethiopia
<QuerySet [<Country: Ethiopia>]>
Not Found: /api/country/ethiopia/
<QuerySet [<Country: Ethiopia>]>
[05/May/2023 18:56:53] "GET /api/country/ethiopia/ HTTP/1.1" 404 23
Not Found: /api/country/ethiopia/
[05/May/2023 18:56:53] "GET /api/country/ethiopia/ HTTP/1.1" 404 23
在Bowser控制台中,当判断console.log(Country Detail)时,我只看到一个空对象.
我也在setCountryDetail(Response1.data)之后try 过sole.log(Response1.Data);但这根本没有显示任何内容……
-EDIT
我现在使用泛型ListAPIView
class CountryDetailView(generics.ListAPIView):
permission_classes = (permissions.AllowAny,)
authentication_classes = (SessionAuthentication,)
serializer_class = CountrySerializer
def get_queryset(self):
param = self.kwargs['name'].capitalize()
formatted_param = param.replace("_", " ").title()
queryset = Country.objects.filter(name=formatted_param)
print(queryset)
print(formatted_param)
return queryset
def list(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = self.get_serializer(queryset, many=True)
print(serializer.data)
return Response(serializer.data)
我也没有拿到404分
但现在我无法访问Reaction组件中的描述. 在记录Country Detail状态时
useEffect(() => {
console.log(countryDetail);
}, [countryDetail]);
我正在获取一个包含一个对象的array.
但是,当加载页面并try 使用{Country Detail[0].Description}访问第一个对象描述码时,我收到一个错误
Cannot read properties of undefined (reading 'description')
好的,那么,当使用{Country Detail.Description}加载页面时,页面上不会出现错误,但也不会出现任何内容.但是,当我在没有重新加载页面并等待 node 编译的情况下键入{Country Detail[0].Description}时,文本显示为?
但当再次重新加载页面时,我再次从上面得到相同的错误.
我真的很困惑!