【问题标题】:TemplateDoesNotExist at /join/home.html (Django)/join/home.html (Django) 中的 TemplateDoesNotExist
【发布时间】:2013-07-29 22:38:28
【问题描述】:

我在这里查看了其他答案,但由于这是我的第一个项目,因此无法掌握如何解决此问题的版本。我可以让 127.0.0.1:8000/admin 正常显示。

我收到此错误:

TemplateDoesNotExist at /join/home.html.
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.5.1
Exception Type: TemplateDoesNotExist
Exception Value:    /join/home.html.

home.html位于/Users/user/Desktop/mvp_landing/static/templates/join

在我的views.py 我有这个:

from django.shortcuts import render_to_response, RequestContext

from .models import Join
from .forms import JoinForm


def home(request):
    form = JoinForm(request.POST or None)
    if form.is_valid():
        new_join = form.save(commit=False)
        new_join.save()
    return render_to_response('/join/home.html.', locals(), context_instance=RequestContext(request))

所以我应该可以接受 settings.pyTEMPLATE_DIRS 的内容,对吧?:

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates"),
)

这是整个settings.py(删除了数据库信息等):

import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG


MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "media")

MEDIA_URL = '/media/'

STATIC_ROOT =  os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "static-only")

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "static"),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)


SECRET_KEY = 'xxxxxxxxxxx'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mvp_landing.urls'

WSGI_APPLICATION = 'mvp_landing.wsgi.application'

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates"),
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    'django.contrib.admindocs',
    'south',
    'join',
)   

urls.py 是这样的:

from django.conf.urls import patterns, include, url
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    (r'static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    (r'media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
    url(r'^$', 'join.views.home', name='home'),

    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

感谢您的帮助。

【问题讨论】:

  • 很可能是您在render_to_respones 调用中的额外尾随.'/join/home.html.' 应该是 '/join/home.html'
  • 感谢彼得的快速回复。我刚刚删除了多余的点,不幸的是我仍然收到错误消息。不过很好抓。我仍在学习语法的复杂性。
  • @ChrisK 这将是第一个/
  • 感谢 Ergusto,这是罪魁祸首。现在,如果我真的知道这就是我要找的东西,我会为自己节省 3 个小时......

标签: django python-2.7 django-templates django-settings


【解决方案1】:

你应该在这里修复路径:

return render_to_response('join/home.html', locals(), context_instance=RequestContext(request))

请注意我是如何删除开头的 / 以及结尾的 .

希望对你有帮助

【讨论】:

  • 谢谢,这就是问题所在。就像我上面提到的那样,我什至对语法都不太了解,所以我什至不确定要寻找什么。
  • Django 的异常页面信息量很大。您可以阅读TemplateDoesNotExist at /join/home.html.,因此,您可能会认为您提供的模板路径有问题,通常在类Unix系统中开头的/是根,因此系统可能正在尝试查找根/join/ 目录下的模板,不存在:)
【解决方案2】:

删除视图模板字符串中的第一个斜杠。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2023-03-27
    • 2022-01-11
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    相关资源
    最近更新 更多