/index.html处的TemplateDoesNotExist-Django中的URL调度程序问题

时间:2018-12-27 07:26:50

标签: python django

尝试使用Django 2.1.4创建简单的应用程序。

项目名称为“ inventory_management”,项目内部的应用程序称为“ gInventory”。

这是我的文件管理的树状视图:

|   db.sqlite3
|   manage.py
|   tree.txt
|   
+---gInventory
|   |   admin.py
|   |   apps.py
|   |   models.py
|   |   tests.py
|   |   urls.py
|   |   views.py
|   |   __init__.py
|   |   
|   +---migrations
|   |       __init__.py
|   |       
|   +---static
|   |   +---css
|   |   |       bootstrap-grid.css
|   |   |       bootstrap-grid.css.map
|   |   |       bootstrap-grid.min.css
|   |   |       bootstrap-grid.min.css.map
|   |   |       bootstrap-reboot.css
|   |   |       bootstrap-reboot.css.map
|   |   |       bootstrap-reboot.min.css
|   |   |       bootstrap-reboot.min.css.map
|   |   |       bootstrap.css
|   |   |       bootstrap.css.map
|   |   |       bootstrap.min.css
|   |   |       bootstrap.min.css.map
|   |   |       style.css
|   |   |       
|   |   \---js
|   |           bootstrap.bundle.js
|   |           bootstrap.bundle.js.map
|   |           bootstrap.bundle.min.js
|   |           bootstrap.bundle.min.js.map
|   |           bootstrap.js
|   |           bootstrap.js.map
|   |           bootstrap.min.js
|   |           bootstrap.min.js.map
|   |           
|   +---templates
|   |       base.html
|   |       index.html
|   |       
|   \---__pycache__
|           urls.cpython-37.pyc
|           views.cpython-37.pyc
|           __init__.cpython-37.pyc
|           
\---inventory_management
    |   settings.py
    |   urls.py
    |   wsgi.py
    |   __init__.py
    |   
    \---__pycache__
            settings.cpython-37.pyc
            urls.cpython-37.pyc
            wsgi.cpython-37.pyc
            __init__.cpython-37.pyc

每当我运行本地服务器并尝试拉出网页时,我都希望index.html出现。相反,我收到一条错误消息,指出“ / index.html处的TemplateDoesNotExist”-该模板不存在,但是我认为该路径有效。

以下是Django尝试加载的错误:

Template-loader postmortem
Django tried loading these templates, in this order:

Using engine django:

django.template.loaders.app_directories.Loader: C:\Users\ateox\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\ateox\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)

urls.py-广告资源管理:

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

urlpatterns = [ #define urls from apps
    path('admin/', admin.site.urls),
    path('', include('gInventory.urls'))
]

urls.py-g库存:

from django.conf.urls import url
from .views import index


urlpatterns =[
    url(r'^$', index, name = "index"),
]

settings.py-广告资源管理

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'thkot@lru9#uo1ub$wpa%u!+)t3lcee^0#f*h8c=imvz@^lq8c'

# 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',
    'inventory',
]

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 = 'inventory_management.urls'

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


# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.1/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/2.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/2.1/howto/static-files/

STATIC_URL = '/static/'

最后是views.py-gInventory

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'index.html')

感谢任何可以提供帮助的人!

3 个答案:

答案 0 :(得分:1)

在您的Settings.py中,您需要用以下代码替换DIRS中的TEMPLATES

'DIRS': [os.path.join(BASE_DIR, 'templates')],

这定义您在项目的基本目录中拥有template文件夹。

您应该遵循的惯例:

  • templates文件夹中创建一个以您的应用程序命名的文件夹,并将html放入其中。

    +---templates/
    |            ginventory/
    |                      base.html
    |                      index.html
    
  • 在您的view函数中,您将html地址指定为:

    def index(request):
        return render(request, 'ginventory/index.html')
    

答案 1 :(得分:0)

将模板文件夹移动到public class ListItem { public ListItem(int ImageSrc, string ItemName) { this.ImageSrc = ImageSrc; this.ItemName = ItemName; } public int ImageSrc { get; set; } public string ItemName { get; set; } } public class MyAdapter : BaseAdapter { List<ListItem> listItems; Context context; public MyAdapter(List<ListItem> listItems, Context context) { this.listItems = listItems; this.context = context; } public override int Count => listItems.Count; public override Java.Lang.Object GetItem(int position) { throw new NotImplementedException(); } public override long GetItemId(int position) { return position; } public override View GetView(int position, View convertView, ViewGroup parent) { convertView = LayoutInflater.From(context).Inflate(Resource.Layout.ListItem, parent, false); TextView textView = convertView.FindViewById<TextView>(Resource.Id.Ls_textview); ImageView imageView = convertView.FindViewById<ImageView>(Resource.Id.Ls_ImageView); //int id=Resource.Drawable.Capture; textView.Text = listItems[position].ItemName; imageView.SetImageResource(listItems[position].ImageSrc); return convertView; } } 目录下

答案 2 :(得分:0)

如果要按照官方文档的建议进行操作,请按照以下步骤操作:

您项目的TEMPLATES设置描述了Django如何加载和 渲染模板。默认设置文件配置了 将APP_DIRS选项设置为True的DjangoTemplates后端。通过 约定DjangoTemplates在以下位置查找“ templates”子目录 每个INSTALLED_APPS。

  1. 因此,请检查您的应用名称是否在INSTALLED_APPS列表中。然后在settings.py内部检查并寻找TEMPLATES,然后检查是否'APP_DIRS': True

  2. 您的模板文件应该在以下路径中:myproject/myapp/templates/myapp/mypage.html这是django在每个应用程序中查找模板的方式。

  3. 最后,您的views函数应以如下形式结束:

    def索引(请求):

    ....

    返回渲染(请求,“ portal / index.html”,上下文)