django templateDoesNotExist at

时间:2014-08-31 19:44:52

标签: python django

我得到了

templatedoesnotexistat / login.html

我复制并粘贴了另一个有效的项目设置,无法找出无法找到模板文件的原因。我已经多次翻过它并复制了路径,所以它们应该是正确的。它让我发疯了

我的文件结构是

-virtual
    -src
    -logins
    -dashboards
    -static
        -templates
            -login.html
        -static
        -static-only
        -media

settings.py

import os
BASE_DIR = '/Users/user/Documents/Python/virtual/'

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'logins',
    'dashboards',
)

ROOT_URLCONF = 'src.urls'

STATIC_URL = '/static/'

TEMPLATE_DIR = (
    '/Users/user/Documents/Python/virtual/src/static/templates',
)

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = '/Users/user/Documents/Python/virtual/src/static/static-only/'
    MEDIA_ROOT = '/Users/user/Documents/Python/virtual/src/static/media/'
    STATICFILES_DIRS = (
        '/Users/user/Documents/Python/virtual/src/static/static/',
    )

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'logins.views.login', name='login'),
    url(r'^/accounts/auth/$', 'logins.views.auth_view', name='auth_view'),
    url(r'^/accounts/dashboard/$', 'dashboards.views.dashboard', name='dashboard'),
    url(r'^/accounts/logout/$', 'logins.views.logout', name='logout'),
    url(r'^/accounts/invalid/$', 'logins.views.invalid', name='invalid'),

    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

views.py

from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf

def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html', c)

def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        return HttpResponseRedirect('/accounts/dashboard')
    else:
        return HttpResponseRedirect('/accounts/invalid')

def logout(request):
    return render_to_response('logout.html')

def invalid(request):
    return render_to_response('invalid.html')

3 个答案:

答案 0 :(得分:2)

您的设置中有TEMPLATE_DIR。它应该是TEMPLATE_DIRS

答案 1 :(得分:0)

我怀疑你的模板目录是否在静态目录中:删除TEMPLATE_DIRS的那部分。

答案 2 :(得分:0)

在Django 1.8中,template_dirs已被弃用,现在预计将使用TEMPLATES定义[" DIRS"]

The DIRS option
Changed in Django 1.8:
This value used to be defined by the TEMPLATE_DIRS setting.

参考https://docs.djangoproject.com/en/1.8/ref/templates/api/#loader-types

将此作为答案发布,因为我浪费了几个小时来确定解决方案。