错误!模板不存在

时间:2016-07-05 17:38:22

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

我一直在尝试使用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'),
]

my views.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上运行

1 个答案:

答案 0 :(得分:0)

在第二个示例中,模板名称应为字符串

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

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