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
在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
我把这个发出来当作答案,因为我花了几个小时才找到解决办法。
0
我觉得你的模板文件夹可能不在静态文件夹里:把TEMPLATE_DIRS中的那部分去掉吧。
2
你的设置里有一个叫 TEMPLATE_DIR
的东西。其实应该是 TEMPLATE_DIRS
。