首页 > 解决方案 > 如何使用 CreateView?django.template.exceptions.TemplateDoesNotExist:django/forms/widgets/text.html

问题描述

我想使用 CreateView。但是,它会生成错误 django.template.exceptions.TemplateDoesNotExist:django/forms/widgets/text.html

根/accounting_report/views.py

from django.views.generic import CreateView
from .models import AccountingReport
class ReportCreateView(CreateView):
    model = AccountingReport
    fields = ['name','receipts','expenses','user_id']
    success_url="accounting_report/"

根/accounting_report/urls.py

from django.urls import path
from accounting_report.views import ReportCreateView
urlpatterns = [
    path('create', ReportCreateView.as_view(), name='accountingreport_form'),

toot/templates/accounting_report/accountingreport_form.html {{ form }} 在模板呈现期间出错

<!DOCTYPE html>
<html>
<head lang="ja">
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
    <form method="post">
    {% csrf_token %}
    {{  form  }}
    <button type="submit">save</button>
    </form>
</body>
</html>

设置.py

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

标签: python

解决方案


尝试将此添加到您的ReportCreateView

template_name = "accounting_report/accountingreport_form.html"

推荐阅读