我有一个完整的解决方案
我的开发环境:
Windows 7,Python 3.5.2,Django 1.11,WAMP 3.0.6(Apache 2.4.23,mod_wsgi)
- 假设您有带有静态文件的error_404.html模板
- 创建下一个目录结构(“mysite” - Django项目根文件夹)
mysite\
mysite\
settings.py
urls.py
views.py
static\
error404\
files\
style.css
image.jpg
templates\
error404\
error_404.html
- mysite\mysite\settings.py
import os
DEBUG = False
TEMPLATES = [{
..
'DIRS': [os.path.join(BASE_DIR, 'templates')],
..
}]
STATIC_URL = '/static/'
STATIC_ROOT = 'FullPathToYourSite.com/mysite/static/'
导入 os
DEBUG = False
TEMPLATES = [{
..
'DIRS': [os.path.join(BASE_DIR, 'templates')],
..
}]
STATIC_URL = '/static/'
STATIC_ROOT = 'FullPathToYourSite.com/mysite/static/'
- mysite\mysite\urls.py
从django.conf.urls导入handler404,handler500
从.导入views
urlpatterns = [..]
handler404 = views.error_404
handler500 = views.error_404
- mysite\mysite\views.py
from django.shortcuts import render
def error_404(request):
return render(request, 'error404/error_404.html')
- "error_404.html"中的一些Jinjo逻辑(伪代码)
{% load staticfiles %}
...
link type="text/css" href="{% static 'error404/files/style.css' %}"
...
img src="{% static 'error404/files/image.jpg' %}"
...
DEBUG = True
的解决方案。 - nirinsanity