简体   繁体   English

模板的Django渲染错误

[英]Django Rendering Error for Template

Issue: 问题:

When running django startproject manage.py 当运行django startproject manage.py

I receive the following error despite paths being correct when I print them to terminal: 尽管将路径打印到终端时路径正确,但仍收到以下错误:

    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'

I'm not sure why there is a ':' in the middle of the path. 我不确定路径中间为什么有一个':'。

I'm following this tutorial for reference here: 我正在关注本教程,以供参考

What works: 什么有效:

I can view admin. 我可以查看管理员。

Background: 背景:

I'm using a Microsoft windows 10 and using anaconda 我正在使用Microsoft Windows 10和anaconda

Using django and a virutal environment I have a django project in: 使用django和虚拟环境,我在django项目中:

projects/demo/ 项目/演示/

in demo I have the following folders: 在演示中,我有以下文件夹:

demo static template manage.py 演示静态模板manage.py

In templates I have the index.html file which I've checked using my browser and it loads. 在模板中,我具有使用浏览器检查过的index.html文件,该文件已加载。

in projects/demo/demo I have settings.py, urls.py,views.py 在项目/演示/演示中,我有settings.py,urls.py,views.py

Here is the major edits I made that are related to the issue: 以下是与该问题相关的主要修改:

in settings.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',
            ],
        },
    },
]

The path is correct to the file. 该路径是正确的文件。

in urls.py: 在urls.py中:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^index/',views.index)
]

In views.py 在views.py中

def index(request):
    return render(request,'templates/index.html',{})

Change this: 更改此:

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

to: 至:

'DIRS': []

And change your view to this: 并将您的视图更改为:

def index(request):
    return render(request, 'index.html', {})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM