午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Django自定義全局403、404、500錯誤頁面的示例代碼

瀏覽:16日期:2024-10-23 09:52:00

自定義模板

403

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>403-禁止訪問</title></head><body>HTTP 403 - 禁止訪問</body></html>

404

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

500

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

編寫視圖

# 全局403、404、500錯誤自定義頁面顯示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_deniedurlpatterns = [ # ...]# 定義錯誤跳轉(zhuǎn)頁面handler403 = permission_deniedhandler404 = page_not_foundhandler500 = page_error

嘗試使用無權(quán)限用戶訪問,看是否會顯示該頁面

如果不對,修改settings.py中的DEBUG的值

DEBUG = False

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

Http404拋出異常

raise Http404(’資源不存在<id:{}>,請訪問 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-無法找到文件</title> <link rel='external nofollow' rel='stylesheet'></head><body><a rel='external nofollow' ><img src='http://www.leifengta.com.cn/bcjs/{% static ’errors/404.gif’ %}'></a><p hidden>{{ exception|safe }}</p><script src='https://code.jquery.com/jquery-3.1.1.min.js'></script><script src='https://rkxy.com.cn/uploads/202009/24/16008951911.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) { // 一鏈接開頭才跳轉(zhuǎn) toastr.warning(’{{ exception|safe }}’, ’跳轉(zhuǎn)中’); setTimeout(function () { //這里寫時間到后執(zhí)行的代碼 $(location).attr(’href’, redirect_url); }, 3000); } })</script></body></html>

后端

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

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

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 91精品国自产 | 中文字幕精品在线播放 | 一级欧美日韩 | 欧美日日| 成人免费毛片观看 | 日韩欧美精品在线 | 日韩欧美三级 | 国产精品国产精品 | 欧美日韩精品免费 | 国产福利视频在线 | 精品乱子伦 | 国产黄频在线观看 | 欧美一级片在线看 | 日韩精品综合 | 婷婷91| 91视频a| 理论片亚洲 | 欧美日本韩国一区 | 亚洲欧美第一页 | 啪啪综合网| 亚洲成人免费观看 | 成人免费xxxxxx视频 | 中文字幕播放 | 亚洲免费网站 | 成人一区二区三区四区 | 精品视频在线播放 | 操碰av | 男人天堂2014 | 久久国产小视频 | 亚洲色综合 | 天天干天天舔 | 伊人22综合| 高清一区二区 | av在线高清观看 | 成人网在线免费观看 | 久久久久国色av免费观看性色 | 天堂网在线观看视频 | 国产不卡免费视频 | 日韩污视频 | 人超碰| 久久免费网 |