2016-10-14 28 views
1

我是noob學習python,當我學習django的模板時,遇到一些錯誤;我使用的是pycharm2016.2.3,python3.5.2,django1.10.1我的django看起來像不能識別模板

這裏是我的目錄列表:

│ 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 /你好,它告訴我這個:enter image description here

看起來好像模板不能被識別。 有人可以給我一種味道嗎? 謝謝!

+0

這沒有什麼關係的模板。 –

+0

@DanielRoseman是不是渲染模板的迴應? – Dodgedoge

回答

1

你缺少一個網址定義:

urlpatterns = [ 
    url(r'^admin/', admin.site.urls), 
    url(r'^hello$/', djangotest.hello), 
] 
+0

它看起來仍然不工作, – Dodgedoge

+0

後,我添加了url定義,它告訴我這個:AttributeError:模塊'djangotest'沒有屬性'你好' – Dodgedoge

+1

OMG,我從你的答案,我shoud添加url(r '^你好$',view.hello),再次感謝! – Dodgedoge

相關問題