Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django got an unexpected keyword argument 'id'

Tags:

python

django

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.

like image 538
Ula Avatar asked May 16 '16 13:05

Ula


2 Answers

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.

like image 66
Klaus D. Avatar answered Oct 03 '22 08:10

Klaus D.


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

def person_detail(request, id):
like image 9
alecxe Avatar answered Oct 03 '22 09:10

alecxe