在django项目上工作,开始使用类视图并为deleteView获取TemplateDoesNotExist

nom7f22z  于 2023-01-03  发布在  Go
关注(0)|答案(1)|浏览(132)

enter image description here
URLS.py

urlpatterns = [
    path('',views.ProductList.as_view(), name="products-page"),
    path('product/<int:pk>', views.SingleProductView.as_view(), name='product-detail-page'),
    path('product-add', views.ProductAddView.as_view(), name='product-add'),
    **path('delete/<int:pk>/', views.ProductDeleteView.as_view(), name='product-delete'),**
    path('update/<int:pk>/', views.ProductUpdateView.as_view(), name='product-update'),

VIEWS.py

class ProductDeleteView(DeleteView):
    model = Product
    template_name = "product/product_delete"
    success_url = reverse_lazy('products-page')
    context_object_name = 'product'

HTML(存在删除链接的位置)

{% for product in products %}
        <div class="col">
            <div class="card mt-3" style="width: 18rem;">
                <div class="card-body">
                  <h6 class="card-title text-wrap" >{{product.name}}</h6>
                  <h6 class="card-subtitle mb-2 text-muted">{{product.description}}</h6>
                  <h6 class="card-text">Costo: ${{product.cost}}</h6>
                    <a href="{{ product.get_absolute_url }}" class="card-link">Edit</a>
                  <a href="{%url "product-delete" product.pk%}" class="card-link">Delete</a>
                </div>
              </div>
        </div>
        {% endfor %}

删除HTML

<form method="POST" class="row g-3">
    <div class="col-auto">
      <h6>Are you sure you want to delete {{product.name}}?:</h6>
      {% csrf_token %}
      <button type="submit" class="btn btn-danger mb-3" value="Delete">Delete</button>
      <button class="btn btn-primary mb-3">Cancel</button>
    </div>
  </form>

看起来很简单,我只想从数据库中删除那个条目。

rkue9o1l

rkue9o1l1#

在views.py中发现错误。template_name未以html结尾。

template_name = "products/product_delete" -- Error
template_name = "products/product_delete.html" -- Fix

谢啦,谢啦

相关问题