Gaurav Gupta
Gaurav Gupta

Reputation: 445

TypeError: detail() missing 1 required positional argument: 'request'

I am new in Django.

Initially I had this function based view -

@api_view(['GET', 'PUT', 'DELETE'])
def detail(self,request, pk):
"""
Retrieve, update or delete a product instance.
"""
try:
    product = latesttrial.objects.get(pk=pk)
    newproduct = latesttrial.objects.all()
except latesttrial.DoesNotExist:
    return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':
    serializer = latestSerializer(product,context={'request': request})
    return Response(serializer.data)

elif request.method == 'PUT':
    serializer = latestSerializer(product, data=request.data,context={'request': request})
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Then it give this error -

TypeError: detail() missing 1 required positional argument: 'pk' 

For this I did these changes according to this answer - missing 1 required positional argument: 'pk'

Then I had this function based view

@api_view(['GET', 'PUT', 'DELETE'])
def detail(request, *args, **kwargs):
"""
Retrieve, update or delete a product instance.
"""
try:
    pk = self.kwargs.get('pk')
    product = latesttrial.objects.get(pk=pk)
    newproduct = latesttrial.objects.all()
except latesttrial.DoesNotExist:
    return Response(status=status.HTTP_404_NOT_FOUND)

if request.method == 'GET':
    pk = self.kwargs.get('pk')
    serializer = latestSerializer(product,context={'request': request})
    return Response(serializer.data)

elif request.method == 'PUT':
    pk = self.kwargs.get('pk')
    serializer = latestSerializer(product, data=request.data,context={'request': request})
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Then I had this error -

TypeError: detail() missing 1 required positional argument: 'request'

This is my view -

from django.conf.urls import url, include
from rest_framework.urlpatterns import format_suffix_patterns

from .views import partial, Detailspartial, detail#

urlpatterns = [
        url(r'partial',partial,name="partial"),
        url(r'pardelete/(?P<pk>[0-9]+)/$', Detailspartial.as_view(), name="Partial details"),
        url(r'detail',detail,name="newfunction"),
]

How should I resolve my problem. Please help!!

Upvotes: 2

Views: 2329

Answers (1)

Charnel
Charnel

Reputation: 4432

You can revert changes to previous version and change this:

url(r'detail',detail,name="newfunction"),

to this:

url(r'detail(?P<pk>[0-9]+)/$',detail,name="newfunction"),

Upvotes: 4

Related Questions