简体   繁体   English

Django CRUD 模板未显示

[英]Django CRUD template not showing up

Im making a CRUD application using class instances.我正在使用类实例制作 CRUD 应用程序。 My app is a very simple accounting app:我的应用程序是一个非常简单的会计应用程序:

Django: 3.03 Python: 3.8.0 Django:3.03 Python:3.8.0

accounts/models.py帐户/模型.py

class Project(models.Model):
    ## normal fields

accounts/views.py帐户/视图.py

class ProjectCreate(CreateView):
    model = Project
    fields = '__all__'

class ProjectUpdate(UpdateView):
    model = Project
    fields = '__all__'

class ProjectDelete(DeleteView):
    model = Project
    success_url = reverse_lazy('project')

accounts/urls.py:帐户/ urls.py:

urlpatterns = [

path('project/create/', project_views.ProjectCreate.as_view(), name='project_create'),
    path('project/<int:pk>/update/', project_views.ProjectUpdate.as_view(), name='project_update'),
    path('project/<int:pk>/delete/', project_views.ProjectDelete.as_view(), name='project_delete'),

]

I have 2 template files under the following directory:我在以下目录下有 2 个模板文件:

accounts
    accounts
        templates
            project
                project_form.html
                project_confirm_delete.html
        models.py
        urls.py
        views.py
        forms.py

When i call the view:当我调用视图时:

http://localhost:8000/accounts/project/create/ http://localhost:8000/accounts/project/create/

I get the following error:我收到以下错误:

47, in select_template
    raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
django.template.exceptions.TemplateDoesNotExist: accounts/project_form.html

settings.py:设置.py:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'accounts.utils.accounts_vars',
            ],
        },
    },
]

I cant figure out why this is not working.我无法弄清楚为什么这不起作用。 Can you please help.你能帮忙吗。 Any help would be much appreciated.任何帮助将非常感激。 Thanks.谢谢。

check your settings.py file检查您的 settings.py 文件

The views is not able to render your template.视图无法呈现您的模板。 Mention templates directory in settings.py and also check wether your template is present or not在 settings.py 中提及模板目录,并检查您的模板是否存在

You are not telling your views which template to use.您没有告诉您的视图使用哪个模板。 You do this by setting the template_name parameter in your view, eg:您可以通过在视图中设置 template_name 参数来完成此操作,例如:

class ProjectUpdate(UpdateView):  
      model = Project 
      fields = '__all__'
      template_name = 'project/project_form.html

' '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM