我正在尝试使用Django和Udemy教程(here)创建第一个网站,我坚持第7课:主页视图。在runserver之后我得到错误:
use FoxTools\FoxText;
use FoxTools\FoxText as text2;
use FoxTools\FoxText as text3;
我创建了应用配置文件,我的结构树看起来像这样:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.2
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'profiles')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/ admin/templates/home.html (File does not exist)
/root/Documents/tryDjango/lib/python3.4/site-packages/django/contrib/auth/templates/home.html (File does not exist)
Traceback:
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/core/ handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/Documents/tryDjango/src/profiles/views.py" in home
7. return render(request, template, context)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/shortcuts.py" in render
67. template_name, context, request=request, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in render_to_string
98. template = get_template(template_name, using=using)
File "/root/Documents/tryDjango/lib/python3.4/site-packages/django/template/loader.py" in get_template
46. raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /
Exception Value: home.html
settings.py
/tryDjango
/src
/tryDjango
urls.py
setting.py
_init_.py
wsgi.py
/profiles
admin.py
models.py
_init_.py
tests.py
views.py
/static
/templates
home.html
urls.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = XXXX
# 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',
'profiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'tryDjango.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 = 'tryDjango.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
TEMPLATE_DIRS = (os.path.join(os.path.dirname(BASE_DIR), 'static', 'templates'), )
views.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'profiles.views.home', name='home'),
]
我也尝试过来自其他的命令,这里是输出:
from django.shortcuts import render
# Create your views here.
def home(request):
context = locals()
template = 'home.html'
return render(request, template, context)`
非常感谢! P.S我使用Debian。
答案 0 :(得分:6)
将模板放在static
中并不是一个好主意,因为互联网上的文件很容易获得,而且您可能不希望暴露您的原始后端走向世界;)
尝试此文件结构(请注意,我已移动templates
目录):
/tryDjango
/src
/tryDjango
urls.py
setting.py
_init_.py
wsgi.py
/templates
home.html
/profiles
admin.py
models.py
_init_.py
tests.py
views.py
Django'} main template loader搜索每个INSTALLED_APPS
的目录,并使用匹配的templates
文件夹中的第一个模板。
如果您依赖(我推荐),您需要将'tryDjango',
添加到INSTALLED_APPS
。
或者,other default template loader会查看您为templates
个文件夹定义的路径。
为此,您需要添加TEMPLATES['dirs']
in Django 1.8+或TEMPLATE_DIRS
in older versions的完整路径。