Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried

我们两清 提交于 2020-01-06 08:43:12

问题


I am trying to add an user when clicking on a link but I have the following error :

Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried: ['todo/(?P[^/]+)/$']

My views.py

def todo_user(request, todo_id):
    todo.username.add(request.user)
    todo.save()
    return render(request, '/')

Template

<a href="{% url 'todo-user' todo.id %}"></a>

Urls.py

path('validate/<todo_id>/', views.todo_user, name='todo-user),

Views.py for the template render :

def home(request, token):
            todo_instance = get_object_or_404(Todo, token=token)
            context = {
                'token': todo_instance.token,
                'name': todo_instance.name,
       }
       return render(request, '/', context)

Thanks to you guys !


回答1:


In your template you're referring to a todo variable:

<a href="{% url 'todo-user' todo.id %}"></a>

but in your context used to render the template, no such variable is defined. Add

'todo': todo_instance

to your context. You can remove 'token' and 'name' and use {{ todo.name }} in you template instead.



来源:https://stackoverflow.com/questions/55302876/reverse-for-todo-user-with-arguments-not-found-1-patterns-tried

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