0

I am trying to add a user when clicking on a link but I'm getting the following error :

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

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
    The error shows that `todo.id` is empty, so you should show the view that is used to render the template in the first place. – dirkgroten Mar 22 '19 at 15:28
  • Thanks ! I have edited the question, i hope Its good. –  Mar 22 '19 at 15:41

1 Answers1

0

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.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42