当我运行程序它给我错误,Reverse for 'update' with arguments '(1,)' not found. 1 pattern(s) tried: ['crud/add/<:id>$']
,我正在加载数据列表,但在<td><a href="{% url 'crud:update' page.id %}">Edit</a></td>
得到错误,这里我已经添加了我的整个代码,任何人都可以帮我解决这个问题?
views.朋友
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader
from .models import Pages
from django.urls import reverse
from django.shortcuts import get_object_or_404, render
def index(request):
pagelist = Pages.objects.all()
context = {'pagelist': pagelist}
return render(request, 'polls/index.html', context)
def add(request):
if request.POST:
title = request.POST.get("title")
permialink = request.POST.get("permialink")
updated_date = request.POST.get("updated_date")
bodytext = request.POST.get("bodytext")
page_data = Pages(title=title,permialink=permialink,updated_date=updated_date,bodytext=bodytext)
page_data.save()
return HttpResponseRedirect("/");
else:
return render(request,'polls/add.html')
的index.html
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}">
<table>
<tr>
<th>Page Title</th>
<th>Update Date</th>
<th>Action</th>
</tr>
{% if pagelist %}
{% for page in pagelist %}
<tr>
<td>{{ page.title }}</td>
<td>{{ page.updated_date }}</td>
<td><a href="{% url 'crud:update' page.id %}">Edit</a></td>
</tr>
{% endfor %}
{% else %}
<tr>
<td colspan="2">Np Pages are available</td>
</tr>
{% endif %}
</table>
URLs.朋友
from django.urls import path
from . import views
app_name = 'crud'
urlpatterns = [
path('',views.index, name='index'),
path('add/',views.add, name='add'),
path('add/<:id>',views.add, name='update'),
]
<:id>
不正确。将其更改为:
path('add/<id>', views.add, name='update'),
或者指定一个path converter。例如,如果id
是一个整数,那么使用:
path('add/<int:id>', views.add, name='update'),