我不确定为什么我的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}时,文本显示为?

但当再次重新加载页面时,我再次从上面得到相同的错误.

我真的很困惑!

推荐答案

判断"埃塞俄比亚"或"埃塞俄比亚"

Javascript相关问答推荐

Tailwind-Elements React集成到ClojureScript

Regex可以 Select 以任何顺序匹配组

如何用变量productId替换 1"

如何在我的Web应用程序中使用JavaScript显示和隐藏喜欢/不喜欢按钮?

试图为每支球队生成类似于2024/25年欧洲足联冠军联赛瑞士系统格式的独特比赛配对

如何使用Paged.js仅渲染特定页面

如何保持子画布元素的1:1宽高比?

foreach循环中的Typescript字符串索引

如何提取Cypress中文本

如何通过在提交时工作的函数显示dom元素?

了解Node.js中的EventEums和浏览器中的addEventEums之间的关系

react—router v6:路由没有路径

为什么当我解析一个promise时,输出处于挂起状态?

虚拟滚动实现使向下滚动可滚动到末尾

无法读取未定义错误的属性路径名''

如何解决useState错误—setSelect Image不是函数''

使用js构造一个html<;ath&>元素并不能使其正确呈现

Reaction组件在本应被设置隐藏时仍显示

使用父标签中的Find函数查找元素

是否可以将异步调用与useState(UnctionName)一起使用