本文介绍了NoReverseMatch:没有参数'(1,)'的'complete'反向。尝试了1个模式:['complete /< todo_id>']的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
url('', views.index, name= 'index'),
url('add', views.addTodo, name ='add'),
url('complete/<todo_id>', views.completeTodo, name='complete'),
url('deletecomplete', views.deleteCompleted, name='deletecomplete'),
url('deleteall', views.deleteAll, name='deleteall')
]

views.py(程序的一部分)

views.py( portion of a program)

def completeTodo(request, todo_id):
todo = Todo.objects.get(pk=todo_id)
todo.complete = True
todo.save()

return redirect('index')

index.html(程序部分)我想这就是问题所在。

index.html(portion of program) I guess this is where the problem is coming.

 <ul class="list-group t20">
                    {% for todo in todo_list %}
                        {% if todo.complete %}
                        <li class="list-group-item todo-completed">{{ todo.text }}</li>
                        {% else %}
                        <a href="{% url 'complete' todo.id %}"><li class="list-group-item">{{ todo.text }}</li></a>
                        {% endif %}
                    {% endfor %}
                </ul>


推荐答案

您的正则表达式表达式错误:

Your regex expression is wrong:

而不是此:

url('complete/<todo_id>', views.completeTodo, name='complete'),

尝试以下操作:

url(r'^complete/(?P<todo_id>\d+)$', views.completeTodo, name='complete'),

或者如果您想使用

Or in case you want to use path

path('complete/<int:todo_id>', views.completeTodo, name='complete'),

EDIT

因为您使用的是Django 1. *,所以不能使用 path()
设置所有URL的正确方法是使用 url 正则表达式

Since you're using Django 1.*, you can't use path()The correct way to set up all your URLs is with url regex expressions

注意

'$':匹配项必须位于字符串的结尾

'$': The match must occur at the end of the string

'\d +':匹配所有数字

'\d+': Match all digits

r 开头代表 regex


url(r'^$', views.index, name= 'index'),
url(r'^add$', views.addTodo, name ='add'),
url(r'^complete/(?P<todo_id>\d+)$', views.completeTodo, name='complete'),
url(r'^deletecomplete$', views.deleteCompleted, name='deletecomplete'),
url(r'^deleteall$', views.deleteAll, name='deleteall')

这篇关于NoReverseMatch:没有参数'(1,)'的'complete'反向。尝试了1个模式:['complete /&lt; todo_id&gt;']的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 20:38