通用路由: #下载文件 url(" media /(?P <path>。*)$ " 、file_down.FileDown.as_view())通用 下载文件函数: from django.utils.http import urlquote from rest_framework.views import APIView from django.shortcuts import render、redirect、HttpResponse from dal import models from django.http import JsonResponse、FileResponse、StreamingHttpResponse import os import xlwt BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__ file__) ))#C:\ Users \ user \ Desktop \ DownTest クラスFileDown(APIView): def get(self、request、path): path = BASE_DIR + " \\ media \\ " + path.replace(" / "、" \\ " ) print(" path " 、path) message = {} file_name = path.split (" \\ ")[- 1 ] print(" file_name " 、file_name) file = open(path.replace(" "、"")、'rb ') #String Replace into file #これはダウンロードファイルである ことをブラウザに通知しますresponse = FileResponse(file) response [ ' Content-Type ' ] = ' application / octet-stream ' response [ ' Content-Disposition ' ] = " attachment; filename = {} " .format(urlquote(file_name))#名前を設定する print(応答) return response
通用settings配置: LANGUAGE_CODE = ' zh-hans ' TIME_ZONE = ' Asia / Shanghai ' USE_I18N = True USE_L10N = True USE_TZ = True #国際化 #https :// docs.djangoproject.com/en/1.11/topics/i18n/ STATIC_URL = ' / static / ' STATICFILES_DIRS = [ os.path.join(BASE_DIR、' static ' )、 os.path.join(BASE_DIR、" media " )、 ] #Django ユーザーはMEDIA_URL = " / media / " と呼ばれるメディアファイルをアップロードします #メディア設定。ユーザーアップロードファイルはデフォルトでこのフォルダーに配置されます MEDIA_ROOT = os.path.join(BASE_DIR、" media " ) REST_FRAMEWORK = { " DEFAULT_AUTHENTICATION_CLASSES " :[]、 " DEFAULT_PERMISSION_CLASSES " :[]、 }
データベース設計: クラスSuggestion(models.Model): "" " プロジェクト提案テーブル " "" name = models.ForeignKey(to = " UserInfo "、verbose_name = " Suggested Person " ) file = models.FileField(upload_to = ' Suggestion / files '、null = False、unique = True、blank = False、verbose_name = " file(クリックしてダウンロード)" ) file_name = models.CharField(max_length = 50、null = False、verbose_name = " file title " ) is_staff = models。BooleanField(verbose_name = " 緊急ですか") is_staf = models.BooleanField(verbose_name = " 使用するかどうか" ) def __str__ (self): return self.file クラスMeta: verbose_name = " プロジェクト提案テーブル" verbose_name_plural = verbose_name db_table = ' 提案'