Django5/helloWorld/views.py

60 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.http import HttpResponse, StreamingHttpResponse, FileResponse
from django.shortcuts import render, redirect
# Create your views here.
def index(request):
# redirect重定向permanent为True时永久重定向
return redirect("/static/new.html", permanent=True)
#return redirect("/blog/2")
# print("页面请求处理中")
# return render(request, 'index.html')
def blog(request, id):
# 输入http://localhost:8000/blog/0会跳转到http://localhost:8000/static/error.html
if id == 0:
return redirect("/static/error.html")
else:
return HttpResponse("id是" + str(id) + "的博客页面")
def blog2(request, year, month, day, id):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + '/' + ' id是' + str(id) + "的博客页面")
def blog3(request, year, month, day):
return HttpResponse(str(year) + '/' + str(month) + '/' + str(day) + "的博客页面")
# 定义文件路径
file_path = "D:\BaiduNetdiskDownload\Acrobat2024(64bit).zip"
# 第一个下载方法
def download_file1(request):
file = open(file_path, 'rb') # 打开文件
response = HttpResponse(file) # 创建HttpResponse对象
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename=file1.zip'
return response
# 第二个下载方法
def download_file2(request):
file = open(file_path, 'rb') # 打开文件
response = StreamingHttpResponse(file) # 创建StreamingHttpResponse对象
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename=file2.zip'
return response
# 第三个下载方法
def download_file3(request):
file = open(file_path, 'rb') # 打开文件
response = FileResponse(file) # 创建FileResponse对象
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename=file3.zip'
return response