Django1.8开发当中的异常处理

在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

转载于:https://my.oschina.net/wowlinda/blog/550797

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值