在views.py中定义了登陆页
def login_page(request):
return render_to_response('html/login.html', locals())
RUN 抛出以上异常 其中的出错提示
Django tried loading these templates, in this order:
•Using loader django.template.loaders.filesystem.Loader:
•Using loader django.template.loaders.app_directories.Loader:
◦D:\Python34\lib\site-packages\django\contrib\admin\templates\html\login.html (File does not exist)
◦D:\Python34\lib\site-packages\django\contrib\auth\templates\html\login.html (File does not exist)
settings.py的设置
TEMPLATE_DIRS = (
# os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates').replace('\\', '/'),
)
BASE_DIRS 应该指向的是project路径,但是很明显异常信息的路径完全不正确
那么这个巨坑是为什么呢?
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',
],
},
},
]
注意看:'DIRS' : []
这个改为 'DIRS' : ['templates']
OK!具体原因还未找到,在那时先解决了
2 异常 CSRF verification failed. Request aborted
解决方法:在页面的form标签中添加{% csrf_token %} 网上大多数的帖子到此为止,结合经验和官方文档,此处的token应该是前一个view传过来的,如果你在你的页面源码中没发现
<input type='hidden' name='csrfmiddlewaretoken' value='L0xZcVD63scuQnvGL0VRoh7MYCJXdMEC' />
之类的隐藏域,则表明在这个页面之前应该有一个参数传过来,views.py中定义:
def login_page(request):
c = {}
c.update(csrf(request))
return render_to_response('html/login.html', c)
再次查看login.html,如果出现隐藏域,则OK