自己关于Django的一些实践

本文介绍了Django中三种响应方式:render(), redirect() 和 HttpResponse() 的使用及区别。通过实例展示了它们各自的特点及应用场景,同时简单提及了如何在本地环境中模拟CDN效果。

一 render() redirect() HttpResponse() 响应 是个什么东西

def login(request):
    if request.method=='POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        userlist=models.UserInfo.objects.filter(username=username,password=password)
        if userlist:
            obj1=redirect('/index/')
            print(obj1,type(obj1))
            return obj1
    obj=render(request,'login.html')
    print(obj,type(obj))
    return obj

  输出:

  这是redirect()            status_code: 302(临时移动) 服务器目前正从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。会自动将请求者转到不同的位置。但由于搜索引擎会继续抓取原有位置并将其编入索引,因此您不应使用此代码来告诉搜索引擎页面或网站已被移动。 

<HttpResponseRedirect status_code=302, "text/html; charset=utf-8", url="/index/"> <class 'django.http.response.HttpResponseRedirect'>

  这是render()             status_code:200(成功)  服务器已成功处理了请求。通常,这表示服务器提供了请求的网页。

<HttpResponse status_code=200, "text/html; charset=utf-8"> <class 'django.http.response.HttpResponse'>

  这是HttpResponse()

<HttpResponse status_code=200, "text/html; charset=utf-8"> <class 'django.http.response.HttpResponse'>

 

二 自己电脑上模拟CDN的效果

  当用Django启动一个项目后,在自己的浏览器输入地址:http://127.0.0.1:8000/static/bootstrap/css/bootstrap.css,就可以依照路径取到相对应的文件,厉害了。

 

转载于:https://www.cnblogs.com/654321cc/p/8028206.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值