模板的Django渲染错误

时间:2016-12-09 04:43:03

标签: python django render

问题:

运行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项目:

项目/演示/

在演示中我有以下文件夹:

演示 静态的 模板 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',{})

1 个答案:

答案 0 :(得分:0)

改变这个:

'DIRS': os.path.join(BASE_DIR,'demo\\template\\'),

为:

'DIRS': []

并将您的观点改为:

def index(request):
    return render(request, 'index.html', {})
相关问题