模板的 Django 渲染错误
Django Rendering Error for Template
问题:
当运行django startproject manage.py
尽管路径正确,但当我将它们打印到终端时,我收到以下错误:
Traceback (most recent call last):
File "pathdjango\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "pathdjango\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "pathdjango\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "fullpathprojects\demo\csdemo\csdemo\views.py", line 6, in index
return render(request,'templates/index.html')
File "pathdjango\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "pathdjango\template\loader.py", line 67, in render_to_string
template = get_template(template_name, using=using)
File "pathdjango\template\loader.py", line 21, in get_template
return engine.get_template(template_name)
File "pathdjango\template\backends\django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "pathdjango\template\engine.py", line 160, in get_template
template, origin = self.find_template(template_name)
File "pathdjango\template\engine.py", line 134, in find_template
name, template_dirs=dirs, skip=skip,
File "pathdjango\template\loaders\base.py", line 38, in get_template
contents = self.get_contents(origin)
File "pathdjango\template\loaders\filesystem.py", line 24, in get_contents
with io.open(origin.name, encoding=self.engine.file_charset) as fp:
IOError: [Errno 22] Invalid argument: u'fullpath\demo\csdemo\:\templates\index.html'
我不确定为什么路径中间有一个“:”。
我正在学习本教程以供参考here:
什么有效:
我可以查看管理员。
背景:
我正在使用 Microsoft windows 10 并使用 anaconda
使用 django 和虚拟环境我有一个 django 项目:
projects/demo/
在演示中我有以下文件夹:
演示
静止的
模板
manage.py
在模板中,我有 index.html 文件,我已使用浏览器检查并加载了它。
在 projects/demo/demo 我有 settings.py, urls.py,views.py
以下是我所做的与该问题相关的主要编辑:
在settings.py中:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': os.path.join(BASE_DIR,'demo\template\'),
'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中:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',views.index)
]
在views.py
def index(request):
return render(request,'templates/index.html',{})
改变这个:
'DIRS': os.path.join(BASE_DIR,'demo\template\'),
至:
'DIRS': []
并将您的观点更改为:
def index(request):
return render(request, 'index.html', {})
问题:
当运行django startproject manage.py
尽管路径正确,但当我将它们打印到终端时,我收到以下错误:
Traceback (most recent call last):
File "pathdjango\core\handlers\exception.py", line 39, in inner
response = get_response(request)
File "pathdjango\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "pathdjango\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "fullpathprojects\demo\csdemo\csdemo\views.py", line 6, in index
return render(request,'templates/index.html')
File "pathdjango\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "pathdjango\template\loader.py", line 67, in render_to_string
template = get_template(template_name, using=using)
File "pathdjango\template\loader.py", line 21, in get_template
return engine.get_template(template_name)
File "pathdjango\template\backends\django.py", line 39, in get_template
return Template(self.engine.get_template(template_name), self)
File "pathdjango\template\engine.py", line 160, in get_template
template, origin = self.find_template(template_name)
File "pathdjango\template\engine.py", line 134, in find_template
name, template_dirs=dirs, skip=skip,
File "pathdjango\template\loaders\base.py", line 38, in get_template
contents = self.get_contents(origin)
File "pathdjango\template\loaders\filesystem.py", line 24, in get_contents
with io.open(origin.name, encoding=self.engine.file_charset) as fp:
IOError: [Errno 22] Invalid argument: u'fullpath\demo\csdemo\:\templates\index.html'
我不确定为什么路径中间有一个“:”。
我正在学习本教程以供参考here:
什么有效:
我可以查看管理员。
背景:
我正在使用 Microsoft windows 10 并使用 anaconda
使用 django 和虚拟环境我有一个 django 项目:
projects/demo/
在演示中我有以下文件夹:
演示 静止的 模板 manage.py
在模板中,我有 index.html 文件,我已使用浏览器检查并加载了它。
在 projects/demo/demo 我有 settings.py, urls.py,views.py
以下是我所做的与该问题相关的主要编辑:
在settings.py中:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': os.path.join(BASE_DIR,'demo\template\'),
'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中:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/',views.index)
]
在views.py
def index(request):
return render(request,'templates/index.html',{})
改变这个:
'DIRS': os.path.join(BASE_DIR,'demo\template\'),
至:
'DIRS': []
并将您的观点更改为:
def index(request):
return render(request, 'index.html', {})