“got an unexpected keyword argument 'ticket_id'”

谁都会走 提交于 2019-12-24 19:39:57

问题


I'm working on a project management app. A project can have tickets, which are tied to the project and in the template below a project is rendered, as well as the project's tickets.

However, I get an error with the code below and I can't figure out what it means and how to solve it:

Exception Value:    show_ticket() got an unexpected keyword argument 'ticket_id'
Exception Location: /Library/Python/2.7/site-packages/Django-1.4.5-py2.7.egg/django/contrib/auth/decorators.py in _wrapped_view, line 20

What can be wrong?

project template:

{% for ticket in tickets %}
    <span>{{ ticket }}</span>
    <a href="{% url show_ticket project.id ticket.id %}">Show ticket</a><br/>
{% endfor %}

urls.py:

url(r'^project/(?P<project_id>\d+)/ticket/(?P<ticket_id>\d+)/$', 'project_app.views.show_ticket', name="show_ticket"),

view:

@login_required
def show_ticket(request, project_id):
    ticket = get_object_or_404(Ticket, pk = ticket_id)

    return render(request, 'projects/show_ticket.html', {"ticket" : ticket})

回答1:


Your show_ticket view only accepts one variable - the project id. You are calling the reverse on that view with two variables - project.id and ticket.id.

Your URL for the view is already setup to accept the ticket_id, so you just need to change your view to accept the ticket_id as well:

def show_ticket(request, project_id, ticket_id):
    ....


来源:https://stackoverflow.com/questions/15166931/got-an-unexpected-keyword-argument-ticket-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!