[Fixed]-Issue with Django URL Mapping/DetailView

1👍

The problem is that you are using kwargs instead of self.kwargs inside the get_context_data method. It should be something like:

def get_context_data(self, **kwargs):
    # You need to call super() here, so that the context from the DetailView is included
    kwargs = super(detailView, self).get_context_data(**kwargs)

    pk = self.kwargs['pk']  # No need for get() here -- if you get a KeyError then you have a problem in your URL config that should be fixe  # this is the primary key from your URL

    # edit kwargs as necessary
    ...
    return kwargs

In the get_context_data method, kwargs are those passed to the method to make up the context. They are different from self.kwargs, which are from the url pattern.

Leave a comment