簡體   English   中英

TemplateDoesNotExist at /file_name 在 django 中出錯

[英]TemplateDoesNotExist at /file_name error in django

我正面臨這個問題,我相信我已經做了我應該做的一切。

這是我的settings.py

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'blog',
    'interconceptapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'interconcept.urls'

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

WSGI_APPLICATION = 'interconcept.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'interconcept/interconcept/static')
]

嘗試編寫一個簡單的函數視圖來呈現索引或登錄頁面。

def index(request):
    return render(request, 'interconcept/index.html')

每當我運行服務器並嘗試訪問該網頁時,我都會收到此錯誤。

TemplateDoesNotExist at /

interconcept/index.html

Request Method:     GET
Request URL:    http://localhost:8000/
Django Version:     3.2.3
Exception Type:     TemplateDoesNotExist
Exception Value:    

interconcept/index.html

Exception Location:     /home/user/interconcept/interconcept-venv/lib/python3.6/site-packages/django/template/loader.py, line 19, in get_template
Python Executable:  /home/user/interconcept/interconcept-venv/bin/python
Python Version:     3.6.9
Python Path:    

['/home/user/interconcept',
 '/usr/lib/python36.zip',
 '/usr/lib/python3.6',
 '/usr/lib/python3.6/lib-dynload',
 '/home/user/interconcept/interconcept-venv/lib/python3.6/site-packages']

Server time:    Thu, 29 Jul 2021 23:45:13 +0000

我已將我的應用程序添加到 settings.py。 我所指的文件存在。 我的模板文件夾有一些其他文件夾,這樣做是為了使我的代碼更整潔且易於調試。

我在模板下有以下文件夾 - interconcept、blog 和 partials。 Partials 由_alert.html、_navbar.html 和_footer.html 組成。interconcept 文件夾由index.html 和其他一些html 文件組成。

我認為問題在於我的目錄搞砸了。 它似乎正在影響其他事情。

您需要通過應用程序名稱引用模板文件。 似乎您的項目名稱是interconcept而您的應用名稱是interconceptapp

def index(request):
    return render(request, 'interconceptapp/index.html')

假設您已將index.html放置在模板目錄下名為interconceptapp的文件夾中,並使用 in interconceptapp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM