Django收到了意外的关键字参数'id'

Django收到了意外的关键字参数'id'

问题描述:

我正在尝试在Django中创建电话簿。
我的urls.py:

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})

来自models.py:

from models.py:

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

和list.html:

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 %}

索引看起来好的,但是当我尝试单击链接以获取person_detail网站时,我收到以下消息:

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


在/ phonebook / 4 /
中的TypeError person_detail()获得了意外的关键字参数'id'
请求方法:GET请求
URL: http://127.0.0.1:8000/phonebook/4/ Django版本:1.9.6
异常类型:TypeError异常值:person_detail()得到了
意外的关键字参数' id'

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'

我在urls.py和get_absolute_url的函数中有'id'参数。我不明白怎么了。

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

您的参数?P< id> $ c URL映射中的$ c>必须与视图 def person_detail(request,person)中的参数匹配:

他们都应该是 id ,或者都是

They should both be id or both person.