0

私はDjango Bookを読んでいます。現在、第 4 章の途中です。current_datetime.html テンプレート ビューをレンダリングできません。私は何を間違っていますか?テンプレート ローダーが正しいディレクトリを見つけていないようです。http://www.djangobook.com/en/2.0/chapter04/

プロジェクトフォルダーにテンプレートディレクトリを作成し、ディレクトリに current_datetime.html を作成しました。

Setting.py
import os.path

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
    # I've also tried: # '/Users/macuser/Python_Projects/django_test1/templates',
)

views.py
from django.shortcuts import render_to_response
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})

urls.py
from django.conf.urls import patterns, include, url
from django_test1.views import hello, my_homepage_view, current_datetime, hours_ahead


urlpatterns = patterns('',
    (r'^$', my_homepage_view),
    (r'^hello/$', hello),
    (r'^time/$', current_datetime),
    (r'^time/plus/(\d{1,2})/$', hours_ahead),)

エラー:

TemplateDoesNotExist at /time/
current_datetime.html
Request Method: GET
Request URL:
Django Version: 1.4
Exception Type: TemplateDoesNotExist
Exception Value:    
current_datetime.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in find_template, line 138
Python Executable:  /usr/bin/python
Python Version: 2.7.1
Python Path:    
['/Users/macuser/Python_Projects/django_test1',
 '/Library/Python/2.7/site-packages/distribute-0.6.27-py2.7.egg',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
 '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
 '/Library/Python/2.7/site-packages']
Server time:    Mon, 23 Jul 2012 20:40:35 -0500
Template-loader postmortem

Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
/Library/Python/2.7/site-packages/django/contrib/auth/templates/current_datetime.html (File does not exist)
4

2 に答える 2

0

これはばかげた間違いでした。私は実際のアプリを作成しませんでした:python manage.pystartapp"appName"。設定ファイルに「assertFalse」を入れたところ、djangoがsettings.pyファイルを使用していないことに気づきました。

于 2012-07-27T08:12:01.260 に答える
0

render_to_Response 形式をチェックしていません https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#render-to-response

それを通過して、 context_instance=RequestContext(request) を応答に追加する必要があります

于 2012-07-24T07:32:34.910 に答える