亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從Django下載sqlite文件

從Django下載sqlite文件

慕的地6264312 2021-03-15 16:13:44
我有使用sqlite作為數據庫的Django應用。我不會制作能夠下載該sqlite文件的視圖。到目前為止,這是我得到的:def backup(request):    """Return Sqlite3 db file, if Sqlite3 db is used."""    import settings    db_engine = settings.DATABASES['default']['ENGINE']    #db_engine = 'JUST_FOR_TESTING'    if db_engine == 'django.db.backends.sqlite3':        db_path = settings.DATABASES['default']['NAME']        response = HttpResponse(mimetype='application/x-sqlite3')        response['Content-Disposition'] = 'attachment; filename=%s' % db_path        response.write(db_path)        return HttpResponse(response)    else:        return HttpResponse("settings.DATABASES['default']['ENGINE'] is %s,<br />\                             only for 'django.db.backends.sqlite3' online backup is posible." % (db_engine))我所缺少的是如何將文件添加為附件。還可以通過某種方式僅下載特定表嗎?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

您只是試圖將文件路徑作為附件發送。


相關代碼是


from django.conf import settings

from django.core.files import File

db_path = settings.DATABASES['default']['NAME']

dbfile = File(open(db_path, "rb"))

response = HttpResponse(dbfile, mimetype='application/x-sqlite3')

response['Content-Disposition'] = 'attachment; filename=%s' % db_path

response['Content-Length'] = dbfile.size


return response


查看完整回答
反對 回復 2021-03-24
  • 2 回答
  • 0 關注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號