Django文件上傳與下載(FileFlid)
本文實(shí)例為大家分享了Django文件上傳與下載的具體代碼,供大家參考,具體內(nèi)容如下
Django1.4
首先是上傳:
#settings.py
MEDIA_ROOT = HERE#定義一個(gè)完整路徑給 MEDIA_ROOT 以便讓 Django在此處保存上傳文件
MEDIA_URL = 'media'#定義 MEDIA_URL 作為該目錄的公共 URL,要確保該目錄對(duì) WEB 服務(wù)器用戶帳號(hào)是可寫(xiě)的
#model.py #coding=utf-8 from django.db import models class User(models.Model): username = models.CharField(max_length = 30) headImg = models.FileField(upload_to = 'update/%Y%m%d') def __unicode__(self): return self.username
#view.py
#coding=utf-8
from django.shortcuts import render_to_response
from django import forms
from django.http import HttpResponse
from django.template import RequestContext
from disk.models import User
# Create your views here.
class UserForm(forms.Form):
username = forms.CharField()
headImg = forms.FileField()
def register(request):
if request.method == "POST":
uf = UserForm(request.POST, request.FILES)
if uf.is_valid():
#獲取表單信息
username = uf.cleaned_data['username']
headImg = uf.cleaned_data['headImg']
#寫(xiě)入數(shù)據(jù)庫(kù)
user = User()
user.username = username
user.headImg = headImg
user.save()
return HttpResponse('upload ok!')
else:
uf = UserForm()
ur= User.objects.order_by('id')
return render_to_response('register.html',{'uf':uf}, context_instance=RequestContext(request))
前臺(tái)使用{{uf.as_ul}}來(lái)展示form,如下:

#register.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<a href="update/20140711/005zEPW4jw1eg3js7sil3g30500824al.gif" rel="external nofollow" >gao</a>
<body>
<h1>register</h1>
<form method="post" enctype="multipart/form-data" >
{% csrf_token %}
{{uf.as_ul}}
<input type="submit" value="ok" />
</form>
</body>
</html>
上傳成功!
數(shù)據(jù)庫(kù)中是這么個(gè)情況:

接下來(lái)是下載
我的文件目錄是:

要想下載你首先要知道,你上傳的東西到了哪個(gè)目錄,涉及到兩個(gè)地方:
MEDIA_ROOT = HERE
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)復(fù)制整個(gè)目錄的方法
這篇文章主要介紹了python實(shí)現(xiàn)復(fù)制整個(gè)目錄的方法,涉及Python中shutil模塊的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
pyinstaller?pathex參數(shù)引發(fā)打包no?module?name異常
這篇文章主要為大家介紹了一個(gè)關(guān)于pyinstaller的?pathex?參數(shù)所引發(fā)的打包執(zhí)行報(bào)no?module?name的異常錯(cuò)誤解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python可視化神器pyecharts繪制儀表盤(pán)
這篇文章主要介紹了Python可視化神器pyecharts繪制儀表盤(pán),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-07-07
pytorch中交叉熵?fù)p失函數(shù)的使用小細(xì)節(jié)
這篇文章主要介紹了pytorch中交叉熵?fù)p失函數(shù)的使用細(xì)節(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
使用Python進(jìn)行數(shù)據(jù)可視化實(shí)現(xiàn)引人注目的視覺(jué)效果
這篇文章主要介紹了使用Python進(jìn)行數(shù)據(jù)可視化實(shí)現(xiàn)引人注目的視覺(jué)效果,您將了解基本的數(shù)據(jù)可視化概念,以及如何創(chuàng)建各種引人注目的圖表和圖形,從而更好地理解和呈現(xiàn)數(shù)據(jù)2023-04-04
給大家整理了19個(gè)pythonic的編程習(xí)慣(小結(jié))
這篇文章主要介紹了給大家整理了19個(gè)pythonic的編程習(xí)慣(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Python3.6簡(jiǎn)單操作Mysql數(shù)據(jù)庫(kù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

