构建网页内容
视图函数的return具有多种响应类型:
上述函数主要来自django.http,该模块是实现响应功能的核心。
实际开发中可用此模块实现文件下载功能,在index的urls.py和views.py添加如下代码:
urls.py
path('download.html', views.download)
views.py
def download(request):
response = HttpResponse(content_type = 'text/csv')
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
writer = csv.writer(response)
writer.writerow(['First row','A','B','C'])
return response