我是一个菜鸟学习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时,它会显示:enter image description here
似乎无法识别模板。 有人能给我一个味道吗? 谢谢!
答案 0 :(得分:1)
您错过了网址定义:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello$/', djangotest.hello),
]