I am trying to retreive data from data from django backend to react but keep getting TypeError: get() missing 1 required positional argument: 'pk'

Viewed 59

I have two concerns here. I am trying to retrieve data from django which contains images and contents. My first concern is whether using "Content-Type": "application/json" is right. The last and major problem is am getting: TypeError: get() missing 1 required positional argument: 'pk'. What could be the issue? Below is the code:

import React, { useEffect, useState} from 'react'
import axios from 'axios'

const GetPostData = () => {
  const [postState, setPostState] = useState({
    loading: true, 
    posts: null
  })

  const config = {
    headers: {
      "Content-Type": "application/json",
      Authorization: `JWT ${localStorage.getItem("access")}`,
       Accept: "application/json",
    },
  };

   useEffect(()=> {
    const res = axios.get(
      `${process.env.REACT_APP_API_URL}/api/list/PostView/`,
      config
    );
    const allPosts = res.data
    setPostState({loading: false, posts: allPosts})
    console.log(res.data)
  }, [setPostState])

  return (
    <div className='container'>
      <h1>Latest Posts</h1>  
      <div>
     
      </div>      
    </div>
  )
 }

 export default GetPostData
1 Answers

The error occurred because I was passing pk as param in put method.I got the solution from this post: missing 1 required positional argument: 'pk'. This error could still be avoided by using REST framework's mixin as well:

class ListingView(mixins.ListModelMixin, generics.GenericAPIView):

queryset = PostList.objects.all()
serializer_class = PostListSerializer

def get(self, request, *args, **kwargs):
    return self.list(request, *args, **kwargs)
Related