I have a django application where I call api as follows: (api.py)
class studentList(APIView):
def get(self, request, pk, pk2, format=None):
student_detail = Student.objects.filter(last_name = pk, campus_id__name = pk2)
serialized_student_detail = studentSerializer(student_detail, many=True)
return Response(serialized_student_detail.data)
and in the urls I do something like the following:
url(r'^api/student/(?P<pk>.+)/(?P<pk2>.+)/$', api.studentList.as_view()),
Now the problem is that my application is a function of search , where it sends the parameters pkand pk2in the api. Sometimes a user can select only one of these parameters to perform a search operation . Therefore, when only one parameter is selected, the url will look something like this:
http://localhost:8000/api/student/
or
http://localhost:8000/api/student//##value of pk2/
So, how can I make the request still work, and how to make the URL so that it accepts even these parameters?
source
share