【发布时间】: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/
app 和 authentication 应用处理身份验证/登录/注销部分。我在从侧边栏 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