【问题标题】:Error! Template does not exist错误!模板不存在
【发布时间】:2016-11-07 15:30:08
【问题描述】:

我一直在尝试使用 django 创建一个简单的 Web 应用程序,但一直收到 TemplateDoesNotExist 错误

我的网址

from django.conf.urls import url, include
from django.contrib import admin

from buisnesscard import views

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

我的意见.py

from django.shortcuts import render
from django.template.loader import get_template


def login(request):
    return render(request, get_template('form.html'), {})

我什至尝试过而不是使用 get_template

from django.shortcuts import render

def login(request):
    return render(request, 'buisnesscard/templates/buisnesscard/form.html', {})

Settings.py

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

尝试将目录更改为

'DIRS' :[ 'buisnesscard/templates/buisnesscard']

我的 html 文件包含一个简单的静态文件,它得到的错误为

form.html
Request Method: GET
Request URL:    http://127.0.0.1:8000/login/
Django Version: 1.10b1
Exception Type: TemplateDoesNotExist
Exception Value:    
form.html
Exception Location: C:\Python27\lib\site-packages\django-1.10b1-py2.7.egg\django\template\loader.py in get_template, line 25
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.9
Python Path:    
['E:\\Django files\\forms',
 'C:\\Python27\\lib\\site-packages\\django-1.10b1-py2.7.egg',
 'C:\\Windows\\SYSTEM32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']
Server time:    Wed, 6 Jul 2016 10:57:11 +0530

目前在 python 版本 2.7 上运行

【问题讨论】:

    标签: python-2.7 django-templates django-views


    【解决方案1】:

    在您的第二个示例中,模板名称应该是一个字符串

    return render(request, 'buisnesscard/form.html', {})
    

    示例教程可能会有所帮助:https://docs.djangoproject.com/en/1.9/intro/tutorial03/#a-shortcut-render

    【讨论】:

    • 纠正该错误仍然给我同样的 TemplateDoesNotExist 错误!帮助
    • 您确定您指向 Django 的文件实际上位于 buisnesscard/templates/buisnesscard/form.html 中吗?
    • 模板文件夹应为小写
    • 将 'buisnesscard/templates/buisnesscard/form.html' 更改为 'buisnesscard/form.html'。很抱歉造成混乱
    猜你喜欢
    • 2014-06-18
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多