我是一个学习python的新手,在学习django模板的时候,我遇到了一些错误,我使用的是pycharm2016.2.3,python3.5.2,django1.10.1
这是我的目录列表:
│ db.sqlite3
│ manage.py
├─djangotest
│ │ settings.py
│ │ urls.py
│ │ view.py
│ │ wsgi.py
│ │ __init__.py
│
└─templates
hello.html
url.py:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
]
setting.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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',
],
},
hello.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>temtest</title>
</head>
<body>
<h1>{{ hello }}</h1>
</body>
</html>
view.py:
#coding:utf-8
from django.http import HttpResponse
from django.shortcuts import render
def hello(request):
context = {}
context['hello'] = 'yes i am'
return render(request,'hello.html',context)
def first_page(request):
info = 'on yes'
return HttpResponse(info)
当我运行并输入127.0.0.1:8000时,我可以成功打开它,但是当我输入127.0.0.1:8000/hello时,显示如下:
似乎无法识别模板。谁能帮我个忙?谢谢你!
您缺少一个url定义:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello$/', djangotest.hello),
]