id (토큰) 및 id (토큰)가없는 내용을 표시하기 위해보기에 하나의 URL 만 사용하는 방법이 있습니까? 예를 들어, 토큰이 제공되지 않은 경우 사용자의 프로필 목록을 표시하는보기가 있습니다. 그렇지 않으면 전달 된 토큰과 함께 특정 사용자를 표시합니다. 여기id가없고 id가없는보기에 대한 URL이 하나만 있습니다.
는보기이다
def get(self, request, token=None, format=None):
"""
Returns a list of profile of user or single user if token is provided
"""
reply={}
try:
profile_instance = Profile.objects.filter(user=self.request.user)
if token:
profile = profile_instance.get(token=token)
reply['data'] = self.serializer_class(profile).data
else:
reply['data'] = self.serializer_class(profile_instance, many=True).data
except:
reply['data']=[]
return Response(reply, status.HTTP_200_OK)
URL이
url(
r'^users/$',
views.UserList.as_view(),
name="user_list"
),
url(
r'^users/(?P<token>[0-9a-z]+)$',
views.UserList.as_view(),
name="user_profile"
),
하나 개의 URL을 가질 수있는 방법이 있나요 이런 식으로 뭔가 줘야 해?
django rest framework에서 views 세트를 사용해야합니다. –