django自定义错误响应

  前提:settings.py

   #debug为true时要配置网站的allowed_hosts域名
# 简单就为"*"
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']
直接templates下书写404.htm,400.html,403.html,500.html

方式1

#第一步:总的urls.py 重写handler函数,(注意要加项目app名  要写在上面)
from django.conf.urls import url
from django.contrib import admin
from app01 import views
# handler404="app01.views.erro"
# handler400="app01.views.erro"
# handler403="app01.views.erro"
# handler500="app01.views.erro" urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^download/', views.Download),#下载
url(r'^file/', views.File.as_view()),#播放
url(r'^tes/', views.tes),#test
url(r'^data/', views.date),#test
]
#第二步:views.py写错误调的视图
from django.http import HttpResponseNotFound
def erro(request):
return HttpResponseNotFound("NOT FOUND!")

方式2

API调用方式

下面是python中会用到的库。
urllib2
httplib2
pycurl
requests
urllib2

#request
import requests, json
github_url = ”
data = json.dumps({‘name’:’test’, ‘description’:’some test repo’})
r = requests.post(github_url, data, auth=(‘user’, ‘*‘))
print r.json
#以上几种方式都可以调用API来执行动作,但requests这种方式代码最简洁,最清晰,建议采用。
#urllib2, urllib
import urllib2, urllib
github_url = ‘https://api.github.com/user/repos’
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, github_url, ‘user’, ‘*‘)
auth = urllib2.HTTPBasicAuthHandler(password_manager) # create an authentication handler
opener = urllib2.build_opener(auth) # create an opener with the authentication handler
urllib2.install_opener(opener) # install the opener…
request = urllib2.Request(github_url, urllib.urlencode({‘name’:’Test repo’, ‘description’: ‘Some test repository’})) # Manual encoding required
handler = urllib2.urlopen(request)
print handler.read()
#httplib2
import urllib, httplib2
github_url = ’
h = httplib2.Http(“.cache”)
h.add_credentials(“user”, “**“, ”
data = urllib.urlencode({“name”:”test”})
resp, content = h.request(github_url, “POST”, data)
print content
#pycurl
import pycurl, json
github_url = ”
user_pwd = “user:*”
data = json.dumps({“name”: “test_repo”, “description”: “Some test repo”})
c = pycurl.Curl()
c.setopt(pycurl.URL, github_url)
c.setopt(pycurl.USERPWD, user_pwd)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, data)
c.perform()

随机推荐

  1. Redis详解(三)------ redis的五大数据类型详细用法

    我们说 Redis 相对于 Memcache 等其他的缓存产品,有一个比较明显的优势就是 Redis 不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据 ...

  2. 开发工具IntelliJ IDEA的安装步骤及首次启动和创建项目

    开发工具IDEA概述 DEA是一个专门针对Java的集成开发工具(IDE),由Java语言编写.所以,需要有JRE运行环境并配置好环境变量.它可以极大地提升我们的开发效率.可以自动编译,检查错误.在公 ...

  3. 微信公众号开发 [05] 微信支付功能开发(网页JSAPI调用)

    1.微信支付的流程 如下三张手机截图,我们在微信网页端看到的支付,表面上看到的是 "点击支付按钮 - 弹出支付框 - 支付成功后出现提示页面",实际上的核心处理过程是: 点击支付按 ...

  4. 工具(3): 转换Excel表格到MarkDown:exceltk

    源码和下载: 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.0.1.3.pkg windows: h ...

  5. tomcat配置详解

    Tomcat Server的结构图如下: 该文件描述了如何启动Tomcat Server <Server>    <Listener />    <GlobaNaming ...

  6. Magento 2 Error: A technical problem with the server created an error. Try again to continue what you were doing. If the problem persists, try again later.

    有时在Magento上进行调试会很烦人,特别是如果出现了一些问题,而不是在你实际编写代码时. 这是Magento管理面板上发生的事情.截至目前,它可能是三个原因之一,让我们讨论一下: 1.管理员密码 ...

  7. <数据结构基础学习>(四)链表 Part 1

    一.链表基础 动态数组.栈.队列底层都是依托静态数组实现的,靠resize来解决固定容量问题. 链表是真正的动态数据结构,是一种最简单的一种动态数据结构. 更深入的理解引用(或者指针). 更深入的理解 ...

  8. 指针运算中的运算符:&和*

    这里&是取地址运算符,*是间接运算符. &a 的运算结果是一个指针,指针的类型是 a 的类型加个 *,指针所指向的类型是 a 的类型,指针所指向的地址嘛,那就是 a 的地址. *p 的 ...

  9. 锁(1):spin_lock & mutex_lock的区别? .

    为什么需要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是可以访问所有内核数据的,因此要对共享数据进行保护,即互斥处理   有哪些内核锁机制? (1)原子操作 atomic ...

  10. MySQL_表锁_lock tables tableName read

    pre.环境准备 1.建立两个表S,T,并插入一些数据 --创建表S create table S(d int) engine=innodb; ); --创建表T create table T(c i ...