1

私はまだ Django に不慣れで、練習として小さな Web サイトを作成しようとしています。ただし、現在このエラーが発生しています。誰かが私が間違っていた場所を説明し、これを修正する方法を教えてくれたら、それは素晴らしいことです! 私は初心者で、ドキュメントが読みにくい場合があります =[

他に何か追加する必要がある場合はお知らせください。

Environment:

Request Method: GET
Request URL: http://localhost:8000/home/

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')

Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/home/bradford/Development/Django/pub_pic/~/Development/Django/pub_pic/templates/homepage_template/home.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/contrib/auth/templates/homepage_template/home.html (File does not exist)



Traceback:
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/bradford/Development/Django/pub_pic/homepage/views.py" in index
  9.    return render(request,'homepage_template/home.html')
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render
  53.     return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  170.         t = get_template(template_name)
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in get_template
  146.     template, origin = find_template(template_name)
File "/home/bradford/Development/Django/django_1.5.1/local/lib/python2.7/site-packages/django/template/loader.py" in find_template
  139.     raise TemplateDoesNotExist(name)

Exception Type: TemplateDoesNotExist at /home/
Exception Value: homepage_template/home.html

という名前のテンプレートがhome.htmlあり、それはディレクトリにありますpub_pic/templates/homepage_template/home.html

私のpub_pic urls.py:

from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
    url(r'home/',include('homepage.urls', namespace = 'home')),
)

私のホームページ urls.py:

from django.conf.urls import patterns, url
from homepage import views

urlpatterns = patterns('',
    url(r'^$', views.index, name = 'index'),



)

ホームページ/views.py:

from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render, render_to_response

#from homepage.models import 

def index(request):
#   template = loader.get_template('homepage/index.html')
    return render(request,'homepage_template/home.html')
4

4 に答える 4

4

受け入れられた回答が示唆するように、設定にフルパスを含めないでください。これは、機能する場合でも、「アンチジャンゴ」の方法です。

Django では、プロジェクトごとに 1 つのテンプレート フォルダーを作成するか、アプリごとに 1 つのテンプレート フォルダーを作成することができます。後者では、アプリをプロジェクトからプロジェクトに移動できますが (これが本来の動作方法です)、前者ではモノリシックな 1 回限りのプロジェクトを簡素化できます。

絶対パスを指定する必要はありません。設定で、プロジェクトに単一のテンプレート ディレクトリが必要な場合は、次のように使用します。

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates')
,)

これは基本的に、テンプレートがメインの BASE_DIR パスにある「templates」というフォルダーにあることを示しています。この例では、manage.py が存在するルート ディレクトリになります。このルート ディレクトリに「templates」というフォルダを作成し、そこに HTML を入れます (必要に応じてサブフォルダに配置します)。

テンプレートは、次のように簡単にロードできます。

templt = get_template('mytemplate.html)

mytemplate.html ファイルは、ルート ディレクトリの templates/ フォルダーに直接存在すると想定されます。サブフォルダーを使用できます。使用する場合は、「mysubdir/mytemplate.html」のように引用符で指定する必要があります。

別の方法として、各アプリに独自のテンプレートを許可することもできます。この場合、設定に次のものが必要です。

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',

        ],

    },

},

]

ここでの鍵は

'APP_DIRS': True,

これは ( Django docs によると)、各アプリで「テンプレート」フォルダーを探すようにシステムに指示します。プロジェクトは次のようになります (プロジェクト名が mybigproject であると仮定)

/home/myaccount/mybigproject/myapp1/templates/

/home/myaccount/mybigproject/myapp2/templates/

対照的に、最初のシナリオ (単一のテンプレート フォルダー) では、次のようになります。

/home/myaccount/mybigproject/templates/

ただし、重要なことは、引き続きそれを次のように参照することです。

templt = get_template('mytemplate.html)

Django は、すべてのアプリ フォルダーを検索します。それでもファイルが見つからないというエラーが表示される場合は、構成の問題です。

フル パスを指定する場合、PC (共有プロジェクトなど)、プロジェクト、またはアプリを移動するときにそのパスを変更する必要があり、これは大惨事です。

于 2016-04-20T13:43:04.577 に答える
3

settings.py ファイルでは、以下のコードを使用する必要があります。

 
#settings.py
OS のインポート
# プロジェクトへの完全なファイルシステム パス。
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "テンプレート"),)
于 2013-10-22T11:14:39.570 に答える
1
Installed Applications:
('homepage_template.apps.Homepage_templateConfig', #add this line
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles')
于 2016-06-04T11:27:57.053 に答える