溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Django自定義全局403、404、500錯(cuò)誤頁(yè)面的示例代碼

發(fā)布時(shí)間:2020-10-09 03:08:06 來(lái)源:腳本之家 閱讀:186 作者:吾星喵 欄目:開(kāi)發(fā)技術(shù)

自定義模板

403

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>403-禁止訪問(wèn)</title>
</head>
<body>
HTTP 403 - 禁止訪問(wèn)
</body>
</html>

404

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>404-無(wú)法找到文件</title>
</head>
<body>
HTTP 404- 無(wú)法找到文件
</body>
</html>

500

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>500-服務(wù)器錯(cuò)誤</title>
</head>
<body>
HTTP 500 - 內(nèi)部服務(wù)器錯(cuò)誤
</body>
</html>

編寫(xiě)視圖

# 全局403、404、500錯(cuò)誤自定義頁(yè)面顯示
def page_not_found(request):
 return render(request, '404.html')


def page_error(request):
 return render(request, '500.html')


def permission_denied(request):
 return render(request, '403.html')

修改url

from .views import page_error, page_not_found, permission_denied


urlpatterns = [
 # ...
]

# 定義錯(cuò)誤跳轉(zhuǎn)頁(yè)面
handler403 = permission_denied
handler404 = page_not_found
handler500 = page_error

嘗試使用無(wú)權(quán)限用戶(hù)訪問(wèn),看是否會(huì)顯示該頁(yè)面

如果不對(duì),修改settings.py中的DEBUG的值

DEBUG = False

注:若是DEBUG=True,有些情況下則不會(huì)生效

Http404拋出異常

raise Http404('資源不存在<id:{}>,請(qǐng)?jiān)L問(wèn) xxx 查看')

模板中捕獲異常信息

使用{{ exception }}即可捕獲異常信息,轉(zhuǎn)換為html代碼{{ exception|safe }},可以根據(jù)這些代碼中的id等,得到跳轉(zhuǎn)的鏈接,參考

<!DOCTYPE html>
{% load static %}
<html lang="en">
<style type="text/css">
 .pic {
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
 }
</style>
<head>
 <meta charset="UTF-8">
 <title>404-無(wú)法找到文件</title>
 <link href="http://cdn.bootcss.com/toastr.js/latest/css/toastr.min.css" rel="external nofollow" rel="stylesheet">
</head>
<body>
<a  rel="external nofollow" ><img class="pic" src="{% static 'errors/404.gif' %}"></a>
<p hidden>{{ exception|safe }}</p>

<script src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="http://cdn.bootcss.com/toastr.js/latest/js/toastr.min.js"></script>
<script>

 toastr.options = { // toastr配置
  "closeButton": true,
  "debug": false,
  "progressBar": true,
  "positionClass": "toast-top-center",
  "showDuration": "400",
  "hideDuration": "1000",
  "timeOut": "7000",
  "extendedTimeOut": "1000",
  "showEasing": "swing",
  "hideEasing": "linear",
  "showMethod": "fadeIn",
  "hideMethod": "fadeOut"
 };

 $(function () {
  let redirect_url = $('#redirect_url').text();
  if (redirect_url.indexOf('//') === 0 || redirect_url.indexOf('http') === 0) { // 一鏈接開(kāi)頭才跳轉(zhuǎn)
   toastr.warning('{{ exception|safe }}', '跳轉(zhuǎn)中');
   setTimeout(function () {
    //這里寫(xiě)時(shí)間到后執(zhí)行的代碼
    $(location).attr('href', redirect_url);
   }, 3000);
  }
 })

</script>
</body>
</html>

后端

raise Http404('訪問(wèn)資源不存在,即將跳轉(zhuǎn) <span id="redirect_url">{}</span>'.format('blog.starmeow.cn'))
那么當(dāng)出現(xiàn)404錯(cuò)誤是,jquery就獲取該di的值,如果是//或者是http開(kāi)頭,表明可能是個(gè)鏈接(后端請(qǐng)限制格式),前端直接跳轉(zhuǎn)

到此這篇關(guān)于Django自定義全局403、404、500錯(cuò)誤頁(yè)面的示例代碼的文章就介紹到這了,更多相關(guān)Django 403、404、500錯(cuò)誤頁(yè)面內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI