【发布时间】:2014-09-30 03:40:57
【问题描述】:
我有一个 django 应用程序,我在其中调用 api,如下所示:(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)
在网址中,我正在执行以下操作:
url(r'^api/student/(?P<pk>.+)/(?P<pk2>.+)/$', api.studentList.as_view()),
现在的问题是我的应用程序有一个 search 函数,它将参数 pk 和 pk2 发送到 api。有时,用户可能只选择其中一个参数来执行搜索操作。因此,当仅选择一个参数时,url 将如下所示:
http://localhost:8000/api/student/##value of pk//
或
http://localhost:8000/api/student//##value of pk2/
那么我将如何使查询仍然有效,以及如何创建一个 url 以便它接受这些作为参数?
【问题讨论】:
标签: python django django-urls