Reputation: 39
code: urls.py
path('tools/<int:rate>/', ToolsView.as_view(), name="get-all-tools"),
path('tools/post/', ToolsView.as_view(), name="save-tool"),
code: views.py
class ToolsView(APIView):
def get(self, request, rate):
objs = ToolsModel.objects.values_list()[:rate]
if objs is None:
return Response(status=status.HTTP_404_NOT_FOUND)
data = []
for obj in objs:
print(obj)
json = {}
json['toolid'] = obj[0]
json['tool_name'] = obj[1]
json['tool_from'] = obj[2]
json['tool_qty'] = obj[3]
json['tool_state'] = obj[4]
data.append(json)
return Response(data=data, status=status.HTTP_200_OK)
def post(self, request):
data = request.data
serialize = ToolsSerializer(data=data)
if serialize.is_valid():
serialize.save()
return Response(status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_404_NOT_FOUND)
Whenever i call for tools/post/
intended to call post method
i get
get() missing 1 required positional argument: 'rate'
but rate parameter is actually needed for 'tools/<int:rate>/'
which invokes my get method ,example,
Needed help with these. Thanks in advance!
Upvotes: 0
Views: 49
Reputation: 1089
if you put http://127.0.0.1:8000/help/tools/post/ into your browser, you will be performing a GET on the URL. In your urls.py you map that route to ToolsView.as_view()
. This allows you to handle the different HTTP Methods via functions. So def post
will get called when a POST is requested. But you are likely doing a GET, which will call the def get(...)
method. But because you aren't passing in a rate
it's missing from the function arguments, hence the error. To test the post
method you need to perform a POST to http://127.0.0.1:8000/help/tools/post/.
Test Code:
import requests
requests.post("http://127.0.0.1:8000/help/tools/post/", data={})
Upvotes: 1