"IsADirectoryError" Django
при разработке сайта на Django,я столкнулся с ошибкой на домашней странице:
IsADirectoryError at /catalog/
[Errno 21] Is a directory: '/home/fedor/Project/django/fourm/fourm/templates'
содержимое site/urls.py:
from django.urls import include
from django.urls import path
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
]
urlpatterns += [
path('catalog/', include('catalog.urls')),
]
urlpatterns += [
path('', RedirectView.as_view(url='/catalog/', permanent=True)),
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
'
содержимое catalog/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
содержимое apps.py :
from django.apps import AppConfig
class CatalogConfig(AppConfig):
name = 'catalog'
содержимое settings.py:
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
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',
'catalog.apps.CatalogConfig',
]
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 = 'fourm.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR)],
'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 = 'fourm.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
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',
},
]
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
# Find templates in the same folder as settings.py.
#TEMPLATE_DIRS = (
# os.path.join(SETTINGS_PATH, 'templates'),
#)
#
TEMPLATE_DIRS = [
'/home/fedor/Project/django/fourm/templates'
]
# Internationalization
# https://docs.djangoproject.com/en/3.1/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.1/howto/static-files/
STATIC_URL = '/static/'
содержимое viwes.py:
from django.shortcuts import render
from .models import Author
def index(request):
num_authors=Author.objects.count()
return render(request, 'fourm/templates/', context={'num_authors':num_authors})
папка проекта:
Site/
catalog/
admin.py
appps.py
models.py
tests.py
urls.py
viwes.py
site/
asgi.py
settings.py
urls.py
wsgi.py
teamplates/
index.html
как можно решить эту проблему?
Ответы (1 шт):
Автор решения: Za Ars
→ Ссылка
def index(request):
num_authors=Author.objects.count()
return render(request, 'fourm/templates/', context={'num_authors':num_authors})
'fourm/templates/' - это директория, укажите файл шаблона относительно папки templates, например, 'index.html'