This is the tools.py
from .models import Note
from .serializers import NoteSerializer
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
def getNoteslist(request):
notes=Note.objects.all().order_by('-updated')
serializer=NoteSerializer(notes, many=True)
return Response(serializer.data)
def NoteDetail(request, pk):
notedetail=get_object_or_404(Note,id=pk)
serializer=NoteSerializer(notedetail, many=False)
return Response(serializer.data)
def createNote(request):
data=request.data
note=Note.objects.create(
title=data['title'],
body=data['body']
)
serializer=NoteSerializer(note, many=False)
return Response(serializer.data)
def updateNote(request, pk):
note=Note.objects.get(id=pk)
serializer=NoteSerializer(instance=note, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
def delete(request, pk):
note=get_object_or_404(Note,id=pk)
note.delete()
return Response('Note is deleted!')
This is the views.py
from .models import Note
from .tools import getNoteslist,NoteDetail,updateNote,createNote,delete
from rest_framework.decorators import api_view
@api_view(['GET'])
def getNotes(request):
return getNoteslist(request)
@api_view(['POST'])
def createNotes(request):
return createNote(request)
@api_view(['GET'])
def getNote(request,pk):
return NoteDetail(request, pk)
@api_view(['POST'])
def updateNote(request, pk):
return updateNote(request, pk)
@api_view(['DELETE'])
def delNote(request, pk):
return delete(request, pk)
This is the urls.py
from django.urls import path
from . import views
from rest_framework.documentation import include_docs_urls
from rest_framework_swagger.views import get_swagger_view
schema_view=get_swagger_view(title='NOTE TAKING APi')
API_TITLE='NOTE TAKING APi'
API_DESCRIPTION='A note taking API for creating and editing notes.'
urlpatterns=[
path('',views.getNotes,name='Notes'),
path('create/',views.createNotes,name='Notes'),
path('<int:pk>/',views.getNote,name='note'),
path('update/<int:pk>/',views.updateNote,name='note'),
path('delete/<int:pk>/',views.delNote,name='note'),
path('docs/',include_docs_urls(title=API_TITLE,
description=API_DESCRIPTION)),
path('swagger-docs/',schema_view)
]
here is the error:
**Internal Server Error: /Note/update/4/
Traceback (most recent call last):
File "C:\Users\Hp\anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\core\handlers\base.py", line 115, in
_get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\core\handlers\base.py", line 113, in
_get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\decorators.py", line 50, in handler
return func(*args, **kwargs)
File "C:\Users\Hp\anaconda3\DJANGO\api\NoteTaking\Note\views.py", line 20, in updateNote
return updateNote(request, pk)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 492, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\views.py", line 391, in initialize_request
return Request(
File "C:\Users\Hp\anaconda3\lib\site-packages\rest_framework\request.py", line 154, in __init__
assert isinstance(request, HttpRequest), (
AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `rest_framework.request.Request`.**
CodePudding user response:
i got the same errors when i tried it.
The only solution i found was, to write everything in the view directly, so changing:
@api_view(['POST'])
def updateNote(request, pk):
return updateNote(request, pk)
to:
@api_view(['POST'])
def updateNote(request, pk):
note=Note.objects.get(id=pk)
serializer=NoteSerializer(instance=note, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
worked for me
i don't know why it has problems calling another function
CodePudding user response:
The issue is that functions have clashing names. Change the name updateNote in either views.py or in tools.py