找不到带有参数“('',)”的“ todo-user”。尝试了1个模式 [英] Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried
本文介绍了找不到带有参数“('',)”的“ todo-user”。尝试了1个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在单击链接时添加用户,但是出现以下错误:
I am trying to add an user when clicking on a link but I have the following error :
'todo-user'的反向找不到带有参数'('',)'的'。尝试了1个模式:['todo /(?P [^ /] +)/ $']
Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried: ['todo/(?P[^/]+)/$']
我的views.py
def todo_user(request, todo_id):
todo.username.add(request.user)
todo.save()
return render(request, '/')
模板
<a href="{% url 'todo-user' todo.id %}"></a>
Urls.py
path('validate/<todo_id>/', views.todo_user, name='todo-user),
模板渲染的Views.py:
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)
谢谢大家!
推荐答案
在您的模板中,您指的是 todo
变量:
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
根据您的情况。您可以删除令牌和名称,并在模板中使用 {{todo.name}}
。
to your context. You can remove 'token' and 'name' and use {{ todo.name }}
in you template instead.
这篇关于找不到带有参数“('',)”的“ todo-user”。尝试了1个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文