我在 django 中使用基于 Class 的视图更新和删除记录时出现 TemplateDoesNotExist 错误

TemplateDoesNotExist error while I am using Class-Based Views in django for updating and deleting a record

在这里,我正在尝试更新和删除产品,但每当我单击更新和删除 Link 按钮时,它都会显示 affiliation/affproduct_form.html 处的 TemplateDoesNotExist 和对于删除,它在 affiliation/affproduct_confirm_delete.html 处显示 TemplateDoesNotExist

我已经创建了表单并使用基于函数的视图完成了添加和查看。 下面是我的 models.py :

#Product details uploaded
class AffProduct(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='foo')
    product_title = models.CharField(max_length=255)
    uid = models.IntegerField(primary_key=True)
    specification = models.CharField(max_length=255)
    sale_price = models.IntegerField()
    discount = models.IntegerField()
    img1 = models.ImageField(max_length=255, null=True, blank=True, upload_to="images/")
    img2 = models.ImageField(max_length=255, null=True, blank=True, upload_to="images/")
    promote_method = models.TextChoices
    terms_conditions = models.CharField(max_length=255, null=True)
    promote_method = models.CharField(
        max_length=20,
        choices=promote_choices,
        default='PPC'
    )
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.product_title

以下是我对更新和删除功能的看法。我没有使用基于 class 的视图来创建和详细信息。但是对于更新和删除,我已经使用基于 class 的视图完成了。

# update view for details
class AffProductUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = AffProduct #model
    fields = ['product_title', 'uid', 'specification', 'sale_price', 'discount', 'img1', 'img2', 'promote_method', 'terms_conditions',] # fields 
    template_name = 'affiliation/affproduct_form.html' # template for updating
    #success_url = "/blink_viewproduct.html" # redirect url

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super().form_valid(form)

    def test_func(self):
        product = self.get_object()
        if self.request.user == product.user:
            return True
        return False

# delete view for details
class AffProductDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    model = AffProduct  # model
    success_url = '/blink_viewproduct.html'  # redirect url

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super().form_valid(form)

    def test_func(self):
        product = self.get_object()
        if self.request.user == product.user:
            return True
        return False


两个模板都是按命名方式创建的,1。 affproduct_form.html 用于更新,以及 2. affproduct_confirm_delete.html。 html 页面都位于我的附属应用程序文件夹中,还有一个副本是模板文件夹。 下面还有我的 urls.py:

urlpatterns = [
    path('', views.blink_network),  # redirect to root - path
    path('blink_network/', views.blink_network, name='blink_network'),
    path('AddNewProduct/', views.AddNewProduct, name='AddNewProduct'),
    path('blink_viewproduct/', views.showproduct),
    path('blink_viewproduct/', views.blink_viewproduct, name='blink_viewproduct'),
    path('update/', views.update, name='update'),
    path('update/<int:pk>/', Update.as_view(), name='Post_update'),
    path('affiliation/<int:pk>/update', AffProductUpdateView.as_view(), name='affproduct-update'),
    path('affiliation/<int:pk>/delete', AffProductDeleteView.as_view(), name='affproduct-delete'),
    path('link/', views.link, name='link'),
    path('link/<int:uid>/', views.link_view, name='link_view'),
]

下面是我的模板目录,里面存放了所有的模板。

这是我的应用程序目录:

下面是我的模板目录:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

能否请您提供您的联盟应用程序模板目录? 使用 django GCBV 时,目录结构应如下所示。

Djangos GCBV 默认编程为在模型目录中查找 templates/<app_name>/<specified_templatename>

https://docs.djangoproject.com/en/3.2/topics/class-based-views/generic-display/#generic-views-of-objects

假设您将默认 app_dirs 设置为 true..
根据 Django..

Thus, when (for example) the APP_DIRS option of a DjangoTemplates backend is set to True in TEMPLATES, a template location could be: /path/to/project/books/templates/books/publisher_list.html

affiliation
|
|--templates
     |
     |affiliation
         |
         |-affproduct_form.html.html
         |
         |--affproduct_confirm_delete.html


此外,success_url 不是模板名称,它需要一个 url 名称。

from django.urls import reverse_lazy

class AffProductUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    ...
    success_url = reverse_lazy('blink_viewproduct') # the url might not be built yet, so while class is being 'defined' @ runtime we call reverse_lazy
    ...

和..

class AffProductDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
    ...
    success_url = reverse_lazy('blink_viewproduct') # the url might not be built yet, so while class is being 'defined' @ runtime we call reverse_lazy
    ...