Django and Tailwind 'TemplateDoesNotExist' error



我遵循本教程:https://www.ordinarycoders.com/blog/article/django-tailwind

我有一个名为"project"的django项目,其中有两个应用程序"app"one_answers"main"。我正在尝试加载'main>模板>main>home.html'。但是我得到了这个错误:

Internal Server Error: /
Traceback (most recent call last): 
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envlibsite-packagesdjangocorehandlersexception.py", line 47, in inner     
response = get_response(request)
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envlibsite-packagesdjangocorehandlersbase.py", line 181, in _get_response 
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envprojectmainviews.py", line 5, in homepage
return render(request = request, template_name="main/home.html")  
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envlibsite-packagesdjangoshortcuts.py", line 19, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envlibsite-packagesdjangotemplateloader.py", line 61, in render_to_string  
template = get_template(template_name, using=using)
File "C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envlibsite-packagesdjangotemplateloader.py", line 19, in get_template      
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: main/home.html       
[17/Nov/2021 11:49:03] "GET / HTTP/1.1" 500 80436

按照教程,我的设置.py'中有:

"""
Django settings for project project.
Generated by 'django-admin startproject' using Django 3.2.9.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = _____________________[blanked out]_____________________
# 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',
'tailwind',
'app',
'main',
]
TAILWIND_APP_NAME = 'app'
NPM_BIN_PATH = r"C:Program Filesnodejsnpm.cmd"
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',
]
TAILWIND_APP_NAME = 'app'
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envprojectmaintemplate'],
'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 = 'project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}

# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/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/3.2/howto/static-files/
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

在我的项目>urls.py我有:

from django.contrib import admin
from django.urls import path, include  #add include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include ('main.urls')),   #add this
]

在我的main>urls.py

from django.urls import path
from . import views
app_name = "main"   

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

main>views.py我有

from django.shortcuts import render
# Create your views here.
def homepage(request):
return render(request = request, template_name="main/home.html")

我已经尝试将main的模板目录添加到settings.中的DIRS中。通过在我的项目中使用另一个Stack链接:Django:TemplateDoesNotExistat/home.html

但是我还没能打开html文件。

非常感谢您的帮助。感谢

嘿,更改settings.py中的这一行从这个

'DIRS': ['C:UsersKaijDocumentsdjangoTestsdjangoTailwind2envprojectmaintemplate'],

到这个

[BASE_DIR/ 'templates']

然后在main目录中创建一个名为templates的文件夹,并在该文件夹中创建另一个名为主的文件夹,这就是home.html所在的位置。所以像这个

project -> main -> templates -> home.html

在名为main的文件夹中,创建一个urls.py并添加这些行

from django.urls import path
from .views import some_view_name
urlpatterns = [
path('', some_view_name, name='home-view')
]

那么主要地->views.py将此行添加到

from django.shortcuts import render
def some_view_name(request):
return render(request, "main/home.html")

这对你来说应该是个问题。

你的目录结构应该像这个

project 
-> project
urls.py
...
-> main
->templates
-> main
-> home.html
manage.py
...

如果这解决了你的问题,请不要忘记接受它作为正确答案。

最新更新