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
就是你打开一个网页,他会自动的跳转到另一个你设定好的网址上去。
基本的代码操作。
上面的网址变成了下面的网址。。。。