mr_bulrathi
mr_bulrathi

Reputation: 554

Got an unexpected keyword argument 'pk'

I have a list of Classes, and i need to get info of certain Class (i.e. student names, Class monitor etc). There is no problem in displaying Classes list (localhost:8000/classes), but when i'm addressing particular class (localhost:8000/classes/nice_guys), i receive class_list() got an unexpected keyword argument 'pk' error.

views.py

def class_list(request):
    classes = Class.objects.all()
    return render(request, 'classes/class_list.html', {'classes': classes})

def class_display(request, pk):
    class_to_display = get_object_or_404(Class, pk=pk)
    return render(request, 'classes/class_display.html', {'class_to_display': class_to_display})

application urls.py

urlpatterns = [
    url(r'(?P<pk>\w+)', views.class_display),
    url(r'', views.class_list),
]

outer urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^classes/(?P<pk>\w+)', include('students.urls')),
    url(r'^classes', include('students.urls')),
    url(r'^$', views.hello_world)
]

Upvotes: 1

Views: 4567

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600051

You're including the students urls twice, once with the pk argument, which makes no sense: that means it will be passed to every view, including the class_list one which isn't expecting it (and the class_display one would receive it twice). Remove that first include.

Upvotes: 3

Related Questions