Django 应用程序未加载并给出 IsADirectoryError 错误

Django app is not loading and giving IsADirectoryError error

我是 django 和 python 的新手。我使用了一个 django 主题来构建一个站点。 Djanog、NGINX、Gunicorn 运行正常。问题是当我尝试使用应用程序 url 加载页面时,它给了我 IsADirectoryError。 以下是所有信息。

基本目录

drwxr-xr-x 6 rwb  root 4096 Jul 30 15:43 netdash_venv/
drwxr-xr-x 3 rwb  root 4096 Aug  5 16:12 projects/core

netdash_venv 是 python 虚拟文件夹,所有项目文件都在 core 文件夹中 项目.

:/opt/netdash/projects$ ll core
total 76
drwxrwxr-x 7 rwb rwb       4096 Aug 11 11:28 ./
drwxr-xr-x 3 rwb root      4096 Aug  5 16:12 ../
drwxrwxr-x 4 rwb rwb       4096 Aug 10 16:30 app/
drwxrwxr-x 4 rwb rwb       4096 Aug  5 16:31 authentication/
drwxrwxr-x 5 rwb rwb       4096 Aug  5 16:17 core/
srwxrwxrwx 1 rwb www-data     0 Aug 11 11:28 core.sock=
-rw-r--r-- 1 rwb rwb      40960 Aug  5 16:39 db.sqlite3
-rwxr-xr-x 1 rwb rwb        624 Aug  5 15:36 manage.py*
drwxrwxr-x 4 rwb rwb       4096 Aug 10 16:00 sei/
drwxrwxr-x 4 rwb rwb       4096 Aug  5 15:48 staticfiles/

appauthentication 应用程序处理 authentication/login/logout 部分。我在从边栏 url.

启动 sei 应用程序时遇到问题

sidebar.html 文件位于 projects/core/core/templates/ 文件夹中。

        <li class="nav-item">
          <a class="sidebar-link" href="/sei">
            <span class="icon-holder">
              <i class="c-red-500 ti-video-camera"></i>
            </span>
            <span class="title">SEI</span>
          </a>
        </li>       

当我点击上面的 href link 时,出现以下错误。

IsADirectoryError at /sei/
[Errno 21] Is a directory: '/opt/netdash/projects/core/core/templates'
Request Method: GET
Request URL:    http://******.net/sei/
Django Version: 2.2
Exception Type: IsADirectoryError
Exception Value:    
[Errno 21] Is a directory: '/opt/netdash/projects/core/core/templates'
Exception Location: /opt/netdash/netdash_venv/lib/python3.5/site-packages/django/template/loaders/filesystem.py in get_contents, line 23
Python Executable:  /opt/netdash/netdash_venv/bin/python3
Python Version: 3.5.2
Python Path:    
['/opt/netdash/projects/core',
 '/opt/netdash/netdash_venv/bin',
 '/usr/lib/python35.zip',
 '/usr/lib/python3.5',
 '/usr/lib/python3.5/plat-x86_64-linux-gnu',
 '/usr/lib/python3.5/lib-dynload',
 '/opt/netdash/netdash_venv/lib/python3.5/site-packages']

settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app',
    'sei'
]
ROOT_URLCONF = 'core.urls'
LOGIN_REDIRECT_URL = "home"   # Route defined in app/urls.py
LOGOUT_REDIRECT_URL = "home"  # Route defined in app/urls.py
TEMPLATE_DIR = os.path.join(BASE_DIR, "core/templates")  # ROOT dir for templates

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

核心 - urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),          # Django admin route 
    path("", include("authentication.urls")), # Auth routes - login / register
    path("", include("app.urls")),            # UI Kits Html files
    path("sei/", include("sei.urls")),
]

应用程序 - urls.py

from django.urls import path, re_path
from app import views

urlpatterns = [

    # The home page
    path('', views.index, name='home'),

    # Matches any html file
    re_path(r'^.*\.*', views.pages, name='pages'),
]

应用-views.py

from django.contrib.auth.decorators import login_required
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse
from django import template

# Create your views here.
@login_required(login_url="/login/")
def index(request):
    return render(request, "index.html")

@login_required(login_url="/login/")
def pages(request):
    context = {}
    # All resource paths end in .html.
    # Pick out the html file name from the url. And load that template.
    load_template = request.path.split('/')[-1]
    html_template = loader.get_template( load_template )
    return HttpResponse(html_template.render(context, request))

sei - urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="sei"),
]

sei - views.py

from django.shortcuts import render
from .models import camera

# Create your views here.
def index(request):

    cameras = camera.objects.all()

    return render(request, "sei.html", {'cameras': cameras})

我试过很多东西。我可以登录和注销,但每当我尝试转到 /sei 时,它都会给我错误。请帮忙

Django 按照输入的顺序对照 url 模式列表检查 url。

您当前的 核心 - urls.py 看起来像:

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

urlpatterns = [
    path('admin/', admin.site.urls),          # Django admin route 
    path("", include("authentication.urls")), # Auth routes - login / register
    path("", include("app.urls")),            # UI Kits Html files
    path("sei/", include("sei.urls")),
]

这意味着 app.urls 将在 sei.urls.

之前被检查

我不是正则表达式专家,但对于 中的 视图,“sei/”似乎与您的 url 匹配应用。然后它尝试加载 sei 作为模板,但不能,因为它是一个目录......“IsADirectoryError at /sei/”。

您可以通过将其替换为 r'^/[^/]*.html' 来改进正则表达式,使其仅匹配 urls指向根文件夹并以 .html

结尾

我推荐 re-ordering 你的 url 以便首先检查更具体的。

新核心 - urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),          # Django admin route 
    path("sei/", include("sei.urls")),
    path("", include("authentication.urls")), # Auth routes - login / register
    path("", include("app.urls")),            # UI Kits Html files
]

这样 sei/ 将首先匹配并加载正确的视图。