-->

Django url pattern multiple Parameters (without pk

2019-07-29 17:06发布

问题:

I'm new to the Django Framework and one thing bothers me.

I want a simple Rest Call:

www.abc.com/users/1/cantonments/1/

If i use 'pk' in the url pattern everything works out of the box (pk, pk1, pk2....). But i have some permission functionality which expects the parameters in kwargs in the form 'upk' and 'cpk' for user and cantonment. So if i change pk to upk everything breaks. Somehow the url needs ONE pk.

This works:

url(r'^users/(?P<pk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),

This doesnt:

url(r'^users/(?P<upk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),

Is there any way to have an url pattern that does not need one entry with pk?

P.S. The error:

Expected view CantonmentDetail to be called with a URL keyword argument named "pk". Fix your URL conf, or set the `.lookup_field` attribute on the view correctly.

EDIT:

My view is simple:

# Authenticated User can show Cantonment Detail
class CantonmentDetail(generics.RetrieveAPIView):
    serializer_class = serializers.CantonmentSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Cantonment.objects.filter(pk=self.kwargs['cpk'])

Edit2:

I changed get_queryset to get object and it works.

def get_object(self):
    queryset = self.filter_queryset(self.get_queryset())
    obj = queryset.get(pk=self.kwargs['cpk'])
    return obj

Edit3: Using

lookup_url_kwarg = "cpk"

in the class works as well.

回答1:

You can send optional pk using get method with your url like

www.abc.com/users/1/cantonments/?&upk=1

and url should be

url(r'^users/(?P<pk>[0-9]+)/cantonments/$',
views.CantonmentDetail.as_view()),

and views.py

def view_name(request, pk=None):
    upk = request.GET.get('upk')


回答2:

May be in your view you are accessing pk variable

urls.py

url(r'^users/(?P<upk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),

views.py

class your_class_name(ListView):
    def view_name(self):
        upk=self.kwargs['upk']
        cpk=self.kwargs['cpk']
        print upk, cpk
        ...

Hope this is helps you



回答3:

The upk doesn't make any difference to the lookup (because a primary key identifies a single object by design).

So for the view, the lookup_field needs to be set to 'cpk' and everything works.



回答4:

Did you changed your view with the new names of the variables? If you have url like this:

url(r'^users/(?P<upk>[0-9]+)/cantonments/(?P<cpk>[0-9]+)/$',
views.CantonmentDetail.as_view()),

You shouls update your view like this:

def view_name(request, upk=None, cpk=None):
    ...