I would like to show the data in the template from the backend by calling the API I made. But the below error shows up. Why it came? Give me a relevant solution... views.py:
class CourseViewSet(ModelViewSet):
queryset = Course.objects.all()
serializer_class = CourseSerializer
def CourseInfo(request):
callApi = request.get('http://127.0.0.1:8000/api/courses/').json()
context = {
"queryset":callApi
}
return render(request, 'insert.html',context)
urls.py:
router = DefaultRouter()
router.register('courses', views.CourseViewSet, basename='course')
urlpatterns = [
path('', views.CourseInfo, name='CourseInfo'),
path('api/', include(router.urls)),
]
models.py:
class Course(models.Model):
name = models.CharField(max_length=250)
class CourseSerializer(serializers.ModelSerializer):
class Meta:
model = Course
fields = "__all__"
Error:
AttributeError at /
'WSGIRequest' object has no attribute 'get'
Request Method: POST
Request URL: http://127.0.0.1:8000/?coursename=Bangla&courseauthor=Mossaddak&courseprice=23322&coursediscount=234&courseduration=23
Django Version: 3.2.3
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'get'
Exception Location: D:\1_WebDevelopment\7_ProgrammingLanguage\1_FrameWork\4_Django-Rest-Frame-work\2_CBVproject\CBVapp\views.py, line 32, in CourseInfo
Python Executable: C:\Users\DCL\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.5
Python Path:
['D:\\1_WebDevelopment\\7_ProgrammingLanguage\\1_FrameWork\\4_Django-Rest-Frame-work\\2_CBVproject',
'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\lib',
'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39',
'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32',
'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\win32\\lib',
'C:\\Users\\DCL\\AppData\\Roaming\\Python\\Python39\\site-packages\\Pythonwin',
'C:\\Users\\DCL\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time: Tue, 25 Oct 2022 23:10:07 0000
CodePudding user response:
Django request module does not have any .get method for API response you should use the python requests module like this
step -1
pip install requests
step - 2
import requests
def SigninView(request):
p=requests.get('https://jsonplaceholder.typicode.com/todos/1')
print(p.json())
return render(request,'index.html',)
for more info visit this link
https://www.geeksforgeeks.org/response-json-python-requests/