Django关闭debug模式

Django关闭debug模式也就是:

在settings.py里面把DEBUG设置成False

DEBUG = False

但是问题就出来了,报500


下面是部分解决方案,在settings.py你应该很容易配置下面这部分代码:


import os
HERE = os.path.dirname(os.path.dirname(__file__))
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = os.path.join(HERE,'media').replace('\\','/')

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = '/media/'

# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(HERE,'static').replace('\\','/')

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

然后,

在你的templates文件夹下(或者其他你自定义的模版目录)

放好相关的500和404的模版,模版名字一定要是500.html,404.html


配置你的urls.py,你应该能看懂这种写法,不影响你本身配置的那部分,只用在后面加上就行了:

from django.conf.urls import patterns, include, url
from django.conf.urls import static
from django.conf import settings
urlpatterns +=patterns('',
                        url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root':settings.STATIC_ROOT}),
                        url(r'^static/<?P<path>.*>$','django.views.static.serve',{'document_root':settings.MEDIA_ROOT}),
                       )



这样你就可以正常访问你的工程和在报错的情况下出现相应的自定义500页面和404页面了。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值