简体   繁体   中英

TemplateDoesNotExist even though the file exists

在此处输入图片说明

I get this error even though the file exists - why?

在此处输入图片说明

Edit:

在此处输入图片说明

在此处输入图片说明

Additional edit:

from django.shortcuts import render

# Create your views here.

from catalog.models import Book, Author, BookInstance, Genre

def index(request):
    """View function for home page of site."""

    # Generate counts of some of the main objects
    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()

    # Available books (status = 'a')
    num_instances_available = BookInstance.objects.filter(status__exact='a').count()

    # The 'all()' is implied by default.    
    num_authors = Author.objects.count()

    context = {
        'num_books': num_books,
        'num_instances': num_instances,
        'num_instances_available': num_instances_available,
        'num_authors': num_authors,
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, 'index.html', context=context)

It says I haven't wrote enough that there's too much code and no description. So I am typing to bypass this error.

Your setup looks correct, except try not to provide a directory. That may be the issue. Here is my setup for one of my projects

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

What could be happening is the DIRS file path may not be pointing to where you thing it is. You can try this in the Django Console

Activate your virtual environment $ source venv/bin/activate Then run python manage.py shell

in the shell:

>>> from project.settings import TEMPLATES
>>> print(TEMPLATES['DIRS'])

Does the output point to where you expect it to?

===== EDIT

I see Django is looking for your template at /catalog/ where in reality, your template actually lives in app/catalog/templates/ so your DIRS directive is not pointing to the correct path.

If you remove the directory in DIRS , Django will traverse your catalog directory for a folder called templates and find your index.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