NoReverseMatch: Reverse for 'complete' with arguments '(1,)' not found. 1 pattern(s) tried: ['complete/']

后端 未结 3 1356
小蘑菇
小蘑菇 2021-01-22 10:34

urls.py

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

urlpatterns = [
url(\'\', views.index, name= \'index\'),
url(\'add\', views.addTodo, name =\'add\')         


        
相关标签:
3条回答
  • 2021-01-22 11:14

    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'),
    0 讨论(0)
  • 2021-01-22 11:24

    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 for 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')
    
    0 讨论(0)
  • 2021-01-22 11:27

    I was facing the same issue, Here is the solution :

    As your model name is Todo (Capital T) you are fetching the ID of each Tudo but in the views, URLs & template you are writing small (t). It becomes case sensitive.

    Change the name (todo_id) to (Todo_id) everywhere in the views URLs and HTML template.

    Here it is :

    urls.py

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

    template.html

    <a href="{% url 'complete' Todo.id %}">   
    

    views.py

    def completeTodo(request, Todo_id):
        todo = Todo.objects.get(pk=Todo_id)
    
    0 讨论(0)
提交回复
热议问题