Python变饼档
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [os.path.join(BASE_DIR, 'bianbingdang/root'),]
- ]
在urls.py文件当中写入如下逻辑
Python变饼档
- from django.conf import urls
- from .views import page_not_found
- urls.handler404 = page_not_found
在views.py定义逻辑如下,也就是说,当发现root目录下存在请求的文件时,就向浏览器返回该页面:
Python变饼档
- import os
- from django.shortcuts import render_to_response
- from .settings import TEMPLATES
- def page_not_found(request, **kwargs):
- root_templates = os.listdir(TEMPLATES[0]['DIRS'][-1])
- print(request.path[1:] in root_templates)
- if request.path[1:] in root_templates:
- print(request.path)
- return render_to_response(request.path[1:])
- return render_to_response('error/404.html')