Django got an unexpected keyword argument 'id'

45,142

Solution 1

Your parameter ?P<id> in the URL mapping has to match the arguments in the view def person_detail(request, person):

They should both be id or both person.

Solution 2

You should fix the view and use the id argument name instead of person:

def person_detail(request, id):

Solution 3

My mistake was I only added the first parameter instead of both,i.e I mentioned def person_detail(request) instead of def person_detail(request,id)

Share:
45,142
Ula
Author by

Ula

Updated on July 27, 2022

Comments

  • Ula
    Ula over 1 year

    I'm trying to create a phonebook in Django. My urls.py:

        urlpatterns = [
        url(r'^$', views.people_list, name='people_list'),
        url(r'^(?P<id>\d)/$', views.person_detail, name='person_detail'),
    ]
    

    views.py:

    def people_list(request):
        people = Person.objects.all()
        return render(request, 'phonebook/person/list.html', {'people': people})
    
    
    def person_detail(request, person):
        person = get_object_or_404(Person, id=person)
        return render(request, 'phonebook/person/detail.html', {'person': person})
    

    from models.py:

    def get_absolute_url(self):
        return reverse('phonebook:person_detail', args=[self.id])
    

    and list.html:

    {% block content %}
    <h1>Phonebook</h1>
    {% for person in people %}
    <h2>
        <a href="{{ person.get_absolute_url }}">
            {{ person.name }} {{ person.last_name }}
        </a>
    </h2>
    <p class="where">
    {{ person.department }}, {{ person.facility }}
        </p>
    {{ person.phone1 }}<br>
    {% endfor %}
    {% endblock %}
    

    The index looks ok but when I try click on links to get person_detail site I get this message:

    TypeError at /phonebook/4/ person_detail() got an unexpected keyword argument 'id' Request Method: GET Request URL: http://127.0.0.1:8000/phonebook/4/ Django Version: 1.9.6 Exception Type: TypeError Exception Value: person_detail() got an unexpected keyword argument 'id'

    I have and 'id' argument in urls.py and in function to get_absolute_url. I don't understand what's wrong.