undefined
undefined

Reputation: 17

Django tamplate can not loaded

I called a template in the django render function, but django cannot find it

here is the django view code:

from django.shortcuts import render

# Create your views here.
def home_page(request):
    return render(request, "index.html")

And after this I tried to check my settings.py and urls.py, but found nothing, and the file is also in the tamplate subfolder of the app

here is the directory structure:

│  .gitignore
│  db.sqlite3
│  funcational_test.py
│  manage.py
│
├─Terminal_equipment_management
│  │  asgi.py
│  │  settings.py
│  │  urls.py
│  │  view.py
│  │  wsgi.py
│  │  __init__.py
│  │
│  └─__pycache__
│
└─webui
    │  admin.py
    │  apps.py
    │  models.py
    │  tests.py
    │  views.py
    │  __init__.py
    │
    ├─migrations
    │
    ├─templates
           index.html

the settings.py is:

INSTALLED_APPS = [
    "api",
    "webui",
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
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',
            ],
        },
    },
]

urls.py is:

from django.contrib import admin
from django.urls import path, include
from webui import views

urlpatterns = [
    path("", views.home_page, name="home_page"),
]

But when I visit localhost:8000/, Django will report TemplateDoesNotExist at / here is some Error Message: Template-loader postmortem Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: C:\ProgramData\Miniconda3\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist) django.template.loaders.app_directories.Loader: C:\ProgramData\Miniconda3\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)

When I copied the files to the above two folders, the template took effect. I can't be sure what happened

By Google Translate

Upvotes: 0

Views: 52

Answers (3)

PoDuck
PoDuck

Reputation: 1454

First, tamplates should be templates. Second, django looks inside a folder under templates for the app name. It seems redundant, and I agree, but that caused me issues when I was first using django.

└─webui
    │
    └─templates
         │
         └─webui
             │
             └─index.html

Upvotes: 0

mka
mka

Reputation: 173

Add Template DIRS:

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

]

Upvotes: 0

Luthfi
Luthfi

Reputation: 394

I think you forgot to put s for template folder names

└─webui
    │
    ├─tamplates
           index.html

Upvotes: 0

Related Questions