简体   繁体   中英

Django CRUD template not showing up

Im making a CRUD application using class instances. My app is a very simple accounting app:

Django: 3.03 Python: 3.8.0

accounts/models.py

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

accounts/views.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:

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:

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/

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:

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

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

You are not telling your views which template to use. You do this by setting the template_name parameter in your view, eg:

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

'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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