Django学习之HttpResponse的一些常用用法功能。(详解!!!)

1.通过HttpResponse下载文件

首先,在创建的django项目下创建一个文件,

views.py中写入代码

def dowmload(request):
    f = open('Kinght_123/math_course.doc','rb')
    res = HttpResponse(f.read(),content_type='application/msword') #content_type是文件打开的形式,需要到MIME手册自行查找
    res['Content-Disposition'] = 'attachment; filename="math_course.doc"'    #filename是给要下载的文件起个名字
    return res

接着在urls.py中写入相应的路径

运行结果:

2.网页的状态码及其修改和加标签

html = ''''
    <head>
        <meta charset="utf-8">>
    </head>
    
    <form method='post' enctype="multipart/form-data">  <!--告诉后台这个表单里有文件-->
        <input type = 'text' name = 'username'>
        <input type = 'password' name = 'password'>
        <input type = 'checkbox' value = "remember me">
        <input type = 'file'     name = 'f_test' >
        <input type = 'submit' value = '登录' >
    
    </form>

'''
    res = HttpResponse(html,content_type='text/plain',status=300) #status是状态码的修改,content_type是显示的格式,
    res.reason_phrase = 'Kinght'   #在状态码上加标签
    return res

运行结果:

3.HttpResponsePermanentRedirect

就是你打开一个网页,他会自动的跳转到另一个你设定好的网址上去。
基本的代码操作。


上面的网址变成了下面的网址。。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kinght_123

我会继续努力创造更多的优秀作品

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值