'{}' 与参数 '('',)' 的反向 NoReverseMatch。: ['(?P<id>[0-9]+)/\Z']

问题描述 投票:0回答:0

NoReverseMatch 在 /Bookstore/book_detail/7/ 找不到带有参数“(”,)“的“edit_book”。尝试了 1 种模式:['Bookstore/edit_book/(?P[0-9]+)/\Z']

网址.py 路径('edit_book/int:id/', views.edit_book, name = 'edit_book'),

# delet book url
       path('delete_book/<int:id>', views.delete_book, name = 'delete_book')

视图.py def edit_book(请求,ID): book = Book.objects.get(id =id) 形式 = EditBookForm(实例 = 书) 如果 request.method == 'POST': form = EditBookForm(request.POST, request.FILES, instance = book) 如果 form.is_valid(): 表格.保存() 返回重定向('家') 上下文={'形式':形式} 返回渲染(请求,'update_book.html',上下文)

删除书,以id为参数

def delete_book(request, id):
    # getting the book to be deleted
    book = Book.objects.get(id=id)
    # checking if the method is POST
    if request.method == 'POST':
        # delete the book
        book.delete()
        # return to home after a success delete
        return redirect('home')
    context = {'book': book}
    return render(request, 'delete_book.html', context)

The template file is here

模板html

Error show in these line of code
编辑 删除

任何人帮助找到解决方案它在问题中都不会出现相反的错误

django templates design-patterns reverse
© www.soinside.com 2019 - 2024. All rights reserved.