【问题标题】:Django app is not loading and giving IsADirectoryError errorDjango 应用程序未加载并给出 IsADirectoryError 错误
【发布时间】:2020-12-01 10:10:11
【问题描述】:

我是 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虚拟文件夹,所有项目文件都在projects下的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 应用处理身份验证/登录/注销部分。我在从侧边栏 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 链接时,我收到以下错误。

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 时,都会出错。请帮忙。

【问题讨论】:

  • 你的模板路径包含“core/core”是否正确?
  • 是的,没错。
  • 您是否尝试过重新排序您的核心 urls.py?将 Sei 的第二行或第一行放在 urlpatterns 列表中。

标签: django django-views django-urls


【解决方案1】:

Django 按照输入的顺序根据 urlpatterns 列表检查 url。

你当前的 core - 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")),
]

这意味着将在 sei.urls 之前检查 app.urls

我不是正则表达式专家,但似乎“sei/”与您在 app 中的 pages 视图的 URL 相匹配。然后它会尝试将 sei 作为模板加载,但因为它是一个目录而无法加载...“IsADirectoryError at /sei/”。

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

我建议重新排序您的网址,以便首先检查更具体的网址。

新核心 - 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/ 将首先匹配并加载正确的视图。

【讨论】:

  • 是的,你是对的。当我首先放置 path("sei/", include("sei.urls")) 时,它会加载页面,但会发生两件事。页面丢失了我正在使用的主题图标,即使没有验证自己的身份,我也可以进入 sei 页面,因为 app.urls 不再检查@login_required。还有其他建议吗?
  • 我也将@login_required 添加到了sei views.py。现在它也检查身份验证。唯一的痛苦是我还必须添加到所有新应用程序中。但没什么大不了的。唯一不起作用的是 SEI 页面丢失了主题图标路径。不过,您的重新订购解决了我的问题。
  • 你的意思是它没有加载样式表吗?如果是这样,这取决于您如何设置它。您可能需要在路径中添加“../”,因为您现在位于“sei/”目录中,上一层。 Django 支持提供静态文件,但这是一个全新的问题。如果您对我的回答解决了原始问题感到满意,请将其标记为已接受的答案。
  • 不,它不是样式表。这是工作。 sei 页面正在加载。这就是我一开始就遇到的问题。谢谢你的帮助。我会将其标记为已接受的答案。
猜你喜欢
  • 1970-01-01
  • 2016-10-05
  • 2017-09-16
  • 2018-09-19
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
相关资源
最近更新 更多