user4584967
user4584967

Reputation:

TemplateDoesNotExist at / Django?

I'm using Python 3.5 and Django 1.9. I know I'm making a stupid error somewhere but I can't seem to find it, any help on where the error can be?

View.py

from django.shortcuts import render
from aircraft.models import Aircraft

def browseaircraft(request):
    all_aircraft = Aircraft.objects.all()
    variables = {'all_aircraft':all_aircraft}
    return render(request, 'templates/browseaircraft.html', variables)

Urls.py

from django.conf.urls import url
from django.contrib import admin

admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', 'aircraft.views.browseaircraft', name='browseaircraft'),

Settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'AviationProject/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',
            ],
            'debug': DEBUG,
        },
    },
]

......

STATIC_URL = '/static/'
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATES_PATH = os.path.join(PROJECT_PATH, "templates")

TEMPLATE_DIRS = (
    TEMPLATES_PATH,
)

My file directory looks something like this:

File directory

Upvotes: 4

Views: 10433

Answers (3)

Remove "templates/" from the second argument of "render()" in "view.py" as shown below:

# "view.py"

from django.shortcuts import render
from aircraft.models import Aircraft

def browseaircraft(request):
    all_aircraft = Aircraft.objects.all()
    variables = {'all_aircraft':all_aircraft}
    # return render(request, 'templates/browseaircraft.html', variables)
    return render(request, 'browseaircraft.html', variables)
                           # ↑↑ "templates/" is removed

Then, make "DIRS" blank in TEMPLATES" and remove "TEMPLATES_PATH" and "TEMPLATE_DIRS" in "settings.py" as shown below:

# "settings.py"

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        #'DIRS': [os.path.join(BASE_DIR, 'AviationProject/templates')],
        'DIRS': [], # Here
        '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',
            ],
            'debug': DEBUG,
        },
    },
]

......

STATIC_URL = '/static/'
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
# TEMPLATES_PATH = os.path.join(PROJECT_PATH, "templates")
    
# TEMPLATE_DIRS = (
#    TEMPLATES_PATH,
# )

Upvotes: 0

Jair
Jair

Reputation: 61

I had the same problem and the answer is to only add in INSTALLED_APPS[] the name of your app, try INSTALLED_APPS['your_app_name'].

Avoid putting INSTALLED_APPS['your_app_name.apps.your_app_nameConfig']. Is not necessary anymore I guess.

Let your Templates DIR[] blank as it is. Django will find automatically.

Upvotes: 4

flowfree
flowfree

Reputation: 16462

In your settings.py, the template directory should be relative to root dir. Replace this line:

'DIRS': [os.path.join(BASE_DIR, 'AviationProject/templates')],

with this:

'DIRS': [os.path.join(BASE_DIR, 'templates')],

Upvotes: 13

Related Questions