欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Django實(shí)現(xiàn)文件分享系統(tǒng)的完整代碼

 更新時(shí)間:2021年05月07日 08:29:42   作者:qian_xun__  
這篇文章主要介紹了Django實(shí)現(xiàn)文件分享系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、效果展示

文件上傳和展示:

upload

image-20210506205404410

文件搜索:

search

文件下載:

download

刪除文件:

delete

二、關(guān)鍵代碼

#urls.py

from django.urls import path,re_path
from .views import HomeView,DisplayView,MyView,SearchView
from . import views

app_name = 'share'
urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    # 當(dāng)用戶發(fā)起主頁的GET請(qǐng)求時(shí)
    # 會(huì)調(diào)用 HomeView的父類的get方法處理
    # 怎么調(diào)用呢,這里需要用到HomeView的父類的as_view方法
    # 此方法會(huì)調(diào)用dispatch方法, 由后者根據(jù)請(qǐng)求類型選擇響應(yīng)的處理函數(shù)
    path('s/<code>', DisplayView.as_view(), name='display'),   #展示上傳成功的文件
    path('my/', MyView.as_view(), name='my'),            #管理文件
    path('search/', SearchView.as_view(), name='search'),    #搜索文件
    re_path(r'^download/(?P<id>\d*$)', views.Download, name='download'),   #下載文件
    re_path(r'^delete/(?P<id>\d*$)', views.DeleteFile, name='delete'),  #刪除文件

]

#views.py

from django.shortcuts import render
from django.views.generic import TemplateView,ListView
import random
import string
import json
from django.http import HttpResponse,HttpResponsePermanentRedirect,StreamingHttpResponse,HttpResponseRedirect
from .models import Upload
import os
# 創(chuàng)建視圖類需要基礎(chǔ)視圖基類
# 例如 TemplateView 就是用于展示頁面的模板視圖基類
# 該類僅提供get方法,用于處理GET請(qǐng)求
# 當(dāng)然也可以自定義其他方法,例如post
# 展示主頁的話,只需要提供模板文件的名字即可
# 當(dāng)客戶端發(fā)起get請(qǐng)求時(shí),由父類的get方法處理請(qǐng)求
# 該屬性用于提供模板文件,在父類的方法中被調(diào)用
class HomeView(TemplateView):
    '''用來展示主頁的視圖類
    '''
    template_name = "base.html"
    def post(self, request):
        if request.FILES:    #如果表單中有文件
            file = request.FILES.get('file')
            code = ''.join(random.sample(string.digits, 8))     #設(shè)置文件的唯一標(biāo)識(shí)code,并且用作文件改名
            name = file.name
            size = int(file.size)
            path = 'filesystem/static/file/' + code + name   #設(shè)置文件保存的路徑,并且更改文件名(防止重名文件)
            with open(path, 'wb') as f:
                f.write(file.read())      #保存文件

            upload = Upload(
                path = path,
                name = name,
                filesize = size,
                code = code,
                pcip = str(request.META['REMOTE_ADDR'])
            )
            upload.save()   #向數(shù)據(jù)庫插入數(shù)據(jù)
            return HttpResponsePermanentRedirect("/share/s/"+code)    #重定向到DisplayView

class DisplayView(ListView):
    '''展示文件的視圖類
    '''
    def get(self, request, code):
        uploads = Upload.objects.filter(code=code)     #顯示出指定文件
        if uploads:
            for upload in uploads:
                upload.dowmloadcount += 1   #記錄訪問次數(shù)
                upload.save()
        return render(request, 'content.html', {'content':uploads, 'host':request.get_host()})

class MyView(ListView):
    '''
    用戶管理視圖類,就是用戶管理文件的那個(gè)頁面的視圖類
    '''
    def get(self, request):
        #ip = request.META['REMOTE_ADDR']
        #uploads = Upload.objects.filter(pcip=ip)  
        uploads = Upload.objects.all()     #查詢所有文件
        for upload in uploads:
            upload.dowmloadcount += 1
            upload.save()
        return render(request, 'content.html', {'content':uploads})   #將所有文件信息渲染展示

class SearchView(ListView):
    '''搜索功能的視圖類
    '''
    def get(self, request):
        code = request.GET.get('kw')   # 獲取關(guān)鍵詞
        u = Upload.objects.filter(name__icontains=str(code))    # 模糊搜索
        # select * from share_upload where name like '%code%';
        data = {}
        if u:
            # 將符合條件的數(shù)據(jù)放到data中
            for i in range(len(u)):           # 循環(huán)輸出查詢的結(jié)果
                u[i].dowmloadcount += 1
                u[i].save()
                data[i]={}
                data[i]['download'] = u[i].dowmloadcount
                data[i]['filename'] = u[i].name
                data[i]['id'] = u[i].id
                data[i]['ip'] = str(u[i].pcip)
                data[i]['size'] = u[i].filesize
                data[i]['time'] = str(u[i].datetime.strftime('%Y-%m-%d %H:%M'))

        return HttpResponse(json.dumps(data), content_type="application/json")


def Download(request, id):
    """
    下載壓縮文件
    :param request:
    :param id: 數(shù)據(jù)庫id
    :return:
    """
    data = Upload.objects.all()
    file_name = ""  # 文件名
    for i in data:
        if i.code == id:  # 判斷id一致時(shí)
            file_name = i.name  # 覆蓋變量

    base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 項(xiàng)目根目錄
    file_path = os.path.join(base_dir, 'filesystem', 'static', 'file', file_name)  # 下載文件的絕對(duì)路徑

    if not os.path.isfile(file_path):  # 判斷下載文件是否存在
        return HttpResponse("Sorry but Not Found the File")

    def file_iterator(file_path, chunk_size=512):
        """
        文件生成器,防止文件過大,導(dǎo)致內(nèi)存溢出
        :param file_path: 文件絕對(duì)路徑
        :param chunk_size: 塊大小
        :return: 生成器
        """
        with open(file_path, mode='rb') as f:
            while True:
                c = f.read(chunk_size)
                if c:
                    yield c
                else:
                    break

    try:
        # 設(shè)置響應(yīng)頭
        # StreamingHttpResponse將文件內(nèi)容進(jìn)行流式傳輸,數(shù)據(jù)量大可以用這個(gè)方法
        response = StreamingHttpResponse(file_iterator(file_path))
        # 以流的形式下載文件,這樣可以實(shí)現(xiàn)任意格式的文件下載
        response['Content-Type'] = 'application/octet-stream'
        # Content-Disposition就是當(dāng)用戶想把請(qǐng)求所得的內(nèi)容存為一個(gè)文件的時(shí)候提供一個(gè)默認(rèn)的文件名
        response['Content-Disposition'] = 'attachment;filename="{}"'.format(file_name)
        
    except:
        return HttpResponse("Sorry but Not Found the File")
    return response

def DeleteFile(request, id):
    '''刪除指定文件'''
    data = Upload.objects.all()
    file_name = ""  # 文件名
    for i in data:
        if i.code == id:  # 判斷id一致時(shí)
            file_name = i.code + i.name  # 覆蓋變量

    base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 項(xiàng)目根目錄
    file_path = os.path.join(base_dir, 'filesystem', 'static', 'file', file_name)  # 下載文件的絕對(duì)路徑
    if os.path.exists(file_path):
        os.remove(file_path)
    file = Upload.objects.get(code=id)
    file.delete()
    return HttpResponseRedirect('/share/my/')

    '''
    except Exception as e:
        return http.HttpResponseForbidden('文件不存在,下載失??!')
        '''

到此這篇關(guān)于Django實(shí)現(xiàn)文件分享系統(tǒng)的完整代碼的文章就介紹到這了,更多相關(guān)Django文件分享系統(tǒng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決Python安裝cryptography報(bào)錯(cuò)問題

    解決Python安裝cryptography報(bào)錯(cuò)問題

    這篇文章主要介紹了解決Python安裝cryptography報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • python中rb含義理解

    python中rb含義理解

    在本篇文章里小編給大家整理的是關(guān)于python中rb含義及用法內(nèi)容,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python+tkinter+mysql做簡單數(shù)據(jù)庫查詢界面

    python+tkinter+mysql做簡單數(shù)據(jù)庫查詢界面

    本文主要介紹了python+tkinter+mysql做簡單數(shù)據(jù)庫查詢界面,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 一篇文章帶你了解python元組基礎(chǔ)

    一篇文章帶你了解python元組基礎(chǔ)

    今天小編就為大家分享一篇關(guān)于Python中的元組介紹,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2021-08-08
  • django使用JWT保存用戶登錄信息

    django使用JWT保存用戶登錄信息

    這篇文章主要介紹了Django使用jwt獲取用戶信息的實(shí)現(xiàn)方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • python簡單實(shí)現(xiàn)最大似然估計(jì)&scipy庫的使用詳解

    python簡單實(shí)現(xiàn)最大似然估計(jì)&scipy庫的使用詳解

    這篇文章主要介紹了python簡單實(shí)現(xiàn)最大似然估計(jì)&scipy庫的使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python pickle 和 shelve模塊的用法

    python pickle 和 shelve模塊的用法

    pickle和shelve模塊都可以把python對(duì)象存儲(chǔ)到文件中,下面來看看它們的用法吧
    2013-09-09
  • Python的join函數(shù)的用法及實(shí)例

    Python的join函數(shù)的用法及實(shí)例

    這篇文章主要詳細(xì)介紹了python的join函數(shù)的用法及實(shí)例,文章中有詳細(xì)的代碼講解,有一定的參考價(jià)值,需要的同學(xué)可以參考閱讀
    2023-04-04
  • numpy降維方法

    numpy降維方法

    本文主要介紹了numpy降維方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Python?OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的詳細(xì)代碼

    Python?OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的詳細(xì)代碼

    這篇文章主要介紹了Python?OpenCV實(shí)現(xiàn)姿態(tài)識(shí)別的方法,本文通過截圖實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02

最新評(píng)論