问题
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( 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(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>
回答1:
Your regex expression is wrong:
Instead of this:
url('complete/<todo_id>', views.completeTodo, name='complete'),
try this:
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
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
Note
'^': The match must start at the beginning of the string or line.
'$': The match must occur at the end of the string
'\d+': Match all digits
The
r
at the beginning stands forregex
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')
回答2:
Well you wrote a path like:
url('complete/<todo_id>/', views.completeTodo, name='complete'),
But here <todo_id>
is part of the url, it does not denote a variable, etc. it means that there is only one url that will match: /complete/<todo_id>
.
In case you use django-2.x, you probably want to use path(..)
instead:
path('complete/<todo_id>', views.completeTodo, name='complete'),
Furthermore in case todo_id
is typically an integer, it is advisable to specify the type:
path('complete/<int:todo_id>', views.completeTodo, name='complete'),
For django-1.x, you can not use such path(..)
s, and in that case you need to write a regular expression, like:
url(r'^complete/(?P<todo_id>[0-9]+)$', views.completeTodo, name='complete'),
来源:https://stackoverflow.com/questions/50767021/noreversematch-reverse-for-complete-with-arguments-1-not-found-1-patter