问题
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