撤消' todo-user'带有参数'('',)'未找到.尝试了1个模式 [英] Reverse for 'todo-user' with arguments '('',)' not found. 1 pattern(s) tried

查看:108
本文介绍了撤消' 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.

这篇关于撤消&amp;#39; todo-user&amp;#39;带有参数&amp;#39;(&amp;#39;&amp;#39;,)&amp;#39;未找到.尝试了1个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆