99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁大數(shù)據(jù)時代5分鐘,帶你快速入門Django文件上傳下載
5分鐘,帶你快速入門Django文件上傳下載
2021-04-09
收藏

來源:AirPython

作者:星安果

5分鐘,帶你快速入門Django文件上傳下載

1. 前言

大家好,我是安果!

文件上傳、下載作為基礎(chǔ)功能,在 Web 項目中非常普遍,Django 項目如何實現(xiàn)文件上傳下載?

本篇文章將帶大家 5 分鐘快速實現(xiàn)文件上傳下載功能

2. 實戰(zhàn)一下

詳細(xì)實現(xiàn)步驟如下( 9 步)

2-1.進(jìn)入虛擬環(huán)境,創(chuàng)建一個項目及 App

workon django3
# 創(chuàng)建項目
django-admin startproject file_up_and_down_demo
# 進(jìn)入項目根目錄
cd file_up_and_down_demo/
# 創(chuàng)建一個App
django-admin startapp index

2-2.創(chuàng)建模板目錄并配置 settings.py

在 index App 下創(chuàng)建一個 templates 文件夾,然后在項目配置文件 settings.py 中配置 App 及模板目錄

# settings.py
# 配置App
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'
django.contrib.contenttypes',

'django.contrib.sessions',
'django.contrib.messages',
'
django.contrib.staticfiles',

'index',
]
TEMPLATES = [
{
'BACKEND': '
django.template.backends.django.DjangoTemplates',

'DIRS': [
# 配置模板目錄
os.path.join(BASE_DIR, 'index/templates')
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'
django.template.context_processors.debug',

'
django.template.context_processors.request',

'
django.contrib.auth.context_processors.auth',

'
django.contrib.messages.context_processors.messages',

],
},
},
]

2-3.創(chuàng)建文件模型,并映射到數(shù)據(jù)庫

以默認(rèn)的 sqlite 為例,在 index App 下的 models.py 中自定義一個代表文件的模型

該模型包含 3 個字段

  • 文件名稱
  • 文件保存路徑
  • 上傳時間

# index App models.py
from django.db import models
from django.utils import timezone
# 文件模型
class FileModel(models.Model):
# 文件名稱
name = models.CharField(max_length=50)
# 文件保存路徑
path = models.CharField(max_length=100)
# 上傳時間
upload_time = models.DateTimeField(default=timezone.now)

然后,在項目根目錄下執(zhí)行下面 2 條命令,將模型結(jié)構(gòu)映射到數(shù)據(jù)庫中

# 數(shù)據(jù)庫映射
Python3 manage.py makemigrations
python3 manage.py migrate

2-4.自定義表單控件

在 index App 下創(chuàng)建一個表單文件 forms.py

在內(nèi)部自定義一個表單類,繼承于 forms.Form

# index App forms.py
from django import forms
class FileForm(forms.Form):
file = forms.FileField(
# 支持多文件上傳
widget=forms.ClearableFileInput(attrs={'multiple': True}),
label='請選擇文件',
)

2-5.添加上傳、下載路由 URL

為上傳、下載功能添加路由 URL

# 項目urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('index.urls'))
]
# index App urls.py
from django.urls import path
from .views import *
urlpatterns = [
# 上傳
path('', index_view, name='index'),
# 下載
path('download/<id>', download_view, name='download')
]

2-6.編寫模板文件

在 index App 的模板文件夾創(chuàng)建一個簡單的模板文件 upload.html

其中

  • form 代表視圖函數(shù)傳過來的表單實體對象
  • form.as_p 代表以字段格式渲染所有的表單元素

# index App upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主頁-上傳文件</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="確定上傳">
</form>
</body>
</html>

2-7.上傳視圖函數(shù)

在 index App 下的 views.py 中編寫上傳功能的視圖函數(shù)

需要注意的是,我們需要提前在項目根目錄創(chuàng)建一個 upload 文件夾,用于存放上傳的文件

# index App views.py
def index_view(request):
"""
上傳文件
:param request:
:return:
"""
if request.method == 'POST':
form = FileForm(request.POST, request.FILES)
if form.is_valid():
# 選擇的文件
files = request.FILES.getlist('file')
# 遍歷寫入到數(shù)據(jù)庫中
for file in files:
# 寫入到數(shù)據(jù)庫中
file_model = FileModel(name=file.name, path=os.path.join('./upload', file.name))
file_model.save()
# 寫入到服務(wù)器本地
destination = open(os.path.join("./upload", file.name), 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
# 提示上傳成功
return HttpResponse('上傳成功!')
else:
form = FileForm()
return render(request, 'upload.html', locals())

2-8.下載視圖函數(shù)

接著,編寫下載功能的視圖函數(shù)

# index App views.py
def download_view(request, id):
"""
下載文件
:param request:
:param id:文件id
:return:
"""
file_result = FileModel.objects.filter(id=id)
# 如果文件存在,就下載文件
if file_result:
file = list(file_result)[0]
# 文件名稱及路徑
name = file.name
path = file.path
# 讀取文件
file = open(path, 'rb')
response = FileResponse(file)
# 使用urlquote對文件名稱進(jìn)行編碼
response['Content-Disposition'] = 'attachment;filename="%s"' % urlquote(name)
return response
else:
return HttpResponse('文件不存在!')

2-9.運行并測試

運行項目,訪問下面的地址,并上傳一個文件

5分鐘,帶你快速入門Django文件上傳下載

使用 Pycharm 打開 sqlite 數(shù)據(jù)庫,發(fā)現(xiàn)成功插入一條文件記錄,并且文件也上傳到 upload 文件夾下

接著訪問下面的地址實現(xiàn)文件下載功能「 其中,file_id 代表文件的 id 值 」

3. 最后

文章通過一個簡單的例子實現(xiàn)了文件的上傳、下載功能,并同步文件記錄到數(shù)據(jù)庫

實際項目中,一般還包含文件列表、文件刪除等功能,這些功能只需要結(jié)合數(shù)據(jù)庫來增刪查改即可實現(xiàn)

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }