收到错误:在Django中使用参数'(1,)'反转'update'

时间:2019-04-26 14:32:53

标签: python django django-urls

当我运行程序时,它给我错误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.py

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.py

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'),
]

1 个答案:

答案 0 :(得分:4)

<:id>不正确。更改为:

path('add/<id>', views.add, name='update'),

或指定一个path converter。例如,如果id是整数,则使用:

path('add/<int:id>', views.add, name='update'),