我知道有人问过,但是,这有所不同,请看下面:我刚刚创建了一个非常简单的测试项目,文件夹结构是这样的:
忽略红色标记,这是因为,据说它没有检测到元编程问题。
为了简单起见,我已经在主页本身调用了视图,主要项目urls.py有网址:
"""testproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, re_path
from test1.views import test1_detail
urlpatterns = [
path('admin/', admin.site.urls),
re_path('^$', test1_detail)
]
tag_detail1视图文件就是这样:
from django.shortcuts import render
from test1.models import model_test1 as Model
from django.shortcuts import render
def test1_detail(request):
tag = Model.objects.get(name__iexact='birlaman')
return render(request,
'test1/test.html',
{'tag':tag})
模型数据库只是一个简单的数据库:
from django.db import models
class model_test1(models.Model):
name = models.CharField(max_length=31)
age = models.TextField()
我在数据库中创建了一些数据:
settings.py是:
"""
Django settings for testproject project.
Generated by 'django-admin startproject' using Django 2.0.1.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
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.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '@i4=#&q%8ib7(oe7h61n5&&t8w89!4jv2#c+r!jk8%cd%)kh$-'
# 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',
'test1',
'test2'
]
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 = 'testproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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 = 'testproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/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.0/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.0/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.0/howto/static-files/
STATIC_URL = '/static/'
base.html位于模板文件夹中,配置如下: os.path.join(BASE_DIR,'template')。它包含简单的html:
<!DOCTYPE html>
<title>
<head>Some head title</head>
</title>
<body>
{%block content%}
Some default content
{%endblock%}
</body>
</html>
base_test1.html只有一行:
{%extends parent_template|default:"base.html"%}
而且test.html只有:
{%extends parent|default:"test1/base_test1.html"%}
{% block content %}
<h1>test succeeded</h1>
<p>
The tag is: {{tag.name}}
</p>
{% endblock %}
现在,与项目运作相关的每个文件都完全在上面给出,您可以验证的所有文件都在正确的位置。现在,如果它是django 2.0中的一个错误,它将是一个不同的东西,但是在我点击之后,世界上的什么给了我以下错误:./ manage.py runserver
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 2.0.1
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'test1',
'test2']
Installed 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']
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: /home/liz/Documents/testproject/templates/test1/test.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/contrib/admin/templates/test1/test.html (Source does not exist)
* django.template.loaders.app_directories.Loader: /home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/contrib/auth/templates/test1/test.html (Source does not exist)
Traceback:
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/liz/Documents/testproject/test1/views.py" in test1_detail
11. {'tag':tag})
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/shortcuts.py" in render
36. content = loader.render_to_string(template_name, context, request, using=using)
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/template/loader.py" in render_to_string
61. template = get_template(template_name, using=using)
File "/home/liz/DocFolder1/DocFolder1/Environments/django2/lib/python3.5/site-packages/django/template/loader.py" in get_template
19. raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /
Exception Value: test1/test.html
请告诉我问题是什么,已经有2天我试图解决这个问题,我已经多次重新制作了这个项目,但它并没有让步。网上有类似的问题,但提到的解决方案不起作用。
EDIT ---------------------------------------------- --- 我在test1文件夹下添加了一个urls.py并添加了代码:
from django.urls import re_path
from organizer.views import test1_detail
urlpatterns = [
re_path('^$', tag_detail)
]
并且在主urls.py中我使用了这个:
from django.contrib import admin
from django.urls import path, re_path
from django.urls import re_path, include
from test1 import urls as test1_url
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('^', include(test1_url))
]
但是,它仍然无法解决问题。
答案 0 :(得分:2)
您尝试从主urls.py加载模板,而不是从test1 app urls.py中加载模板。因此,模板加载器不会查看test1 / templates。
请注意错误消息:
django.template.loaders.filesystem.Loader: /home/liz/Documents/testproject/templates/test1/test.html (Source does not exist)
从应用程序中调用视图或将模板移动到testproject / templates文件夹。