/join/home.html(Django)上的TemplateDoesNotExist

时间:2013-07-29 23:25:16

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

我在这里已经完成了其他答案但是无法掌握如何修复我的这个问题的版本,因为这是我的第一个项目。我可以得到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.py TEMPLATE_DIRS中{1}}的内容感到满意,对吧?:

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

感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

你应该在这里修改路径:

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

请注意我在开头删除/的方式以及尾随.

希望这有帮助