3

I am currently trying to build an API using the Django Rest Framework. Currently I want to be able to have multiple pks in a single url, but when I try I get an error:

django.core.exceptions.ImproperlyConfigured: "^video/(?P[0-9]+)/quiz/(?P[0-9]+)/list/$" is not a valid regular expression: redefinition of group name 'pk' as group 2; was group 1 at position 31

Here are my URLS:

url(r'^video/(?P<pk>[0-9]+)/quiz/(?P<pk>[0-9]+)/list/$',
        views.QuizList.as_view(),
        name='quizzes-list'),
url(r'^video/(?P<pk>[0-9]+)/quiz/(?P<pk>[0-9]+)/detail/$',
        views.QuizDetail.as_view(),
        name='quizzes-detail'),

I am going to have multiple quizzes for a video and want to be able to reach them through a certain pk in my url.

Is there a simple method to do this or am missing the proper way to do this?

Thank you

Edit* Here are my views:

class VideoList(generics.ListCreateAPIView):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer


class VideoDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Video.objects.all()
    serializer_class = VideoSerializer


# quiz
class QuizList(generics.ListCreateAPIView):
    queryset = Quizzes.objects.all()
    serializer_class = QuizSerializer


class QuizDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Quizzes.objects.all()
    serializer_class = QuizSerializer

Updated urls:

    ###
    # Quiz urls
    ###

    url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/list/$',
        views.QuizList.as_view(),
        name='quizzes-list'),
    url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/detail/$',
        views.QuizDetail.as_view(),
        name='quizzes-detail'),

Updated views:

class QuizList(generics.ListCreateAPIView):
    queryset = Quizzes.objects.all()
    serializer_class = QuizSerializer

    def get(self, request, *args, **kwargs):
        pk1 = kwargs.get('pk1', None)
        pk2 = kwargs.get('pk2', None)
        print(pk1)
        print(pk2)
        return self.list(request, *args, **kwargs)

New Error:

Could not resolve URL for hyperlinked relationship using view name "quizzes-list". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field.

1 Answer 1

6

You must give a unique name for the Named capturing group.

url(r'^video/(?P<pk1>[0-9]+)/quiz/(?P<pk2>[0-9]+)/list/$',
    views.QuizList.as_view(),
    name='quizzes-list'),

Access the corresponding pk* values in the view through,

pk1 = kwargs.get('pk1', None)
pk2 = kwargs.get('pk2', None)

ex:

# quiz
class QuizList(generics.ListCreateAPIView):
    queryset = Quizzes.objects.all()
    serializer_class = QuizSerializer

    def get(self, *args, **kwargs):
        pk1 = kwargs.get('pk1', None)
        pk2 = kwargs.get('pk2', None)
        print pk1
        print pk2
        return super(QuizList, self).get(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

3 Comments

I'm still not understanding. I'm new to this. I posted my views. How would I implement the corresponding pk* to them?
I updated everything, but am now getting a new error which I posted above. I'm confused about the error because 'quizzes-list' is listed in the url name and I have no lookup_field. Any direction? Appreciate all the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.