Django如何关闭debug
silencement
2019-08-19 09:14:145534浏览 · 0收藏 · 0评论
配置Django框架为生产环境关闭Debug
Django在Debug模式下静态资源可以正常访问时,但设置为False后,页面就无法请求到静态资源,需要以下几步处理方式:
1. 设置允许访问的域名
2. 添加url映射至静态文件路径
3. 设置异常处理页面
配置settings.py
# 允许所有外部IP访问ALLOWED_HOSTS = ['*'] ... ...# 设置静态资源路径STATIC_URL = '/static/'STATIC_ROOT = 'static'
配置urls.py
from django.conf.urls import urlfrom django.views import staticfrom django.conf import settingsfrom . import view urlpatterns = [ path('admin/', admin.site.urls), path('', view.index), ........ # 识别静态资源 url(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static') ]# 配置异常页面handler403 = view.page_permission_denied handler404 = view.page_not_found handler500 = view.page_inter_error
配置view.py
from django.shortcuts import render def page_permission_denied(request): return render(request, '403.html') def page_not_found(request): return render(request, '404.html') def page_inter_error(request): return render(request, '500.html')
关注公众号,随时随地在线学习