在使用者資訊管理的過程中,如果需要修改使用者的頭像就需要把使用者上傳的頭像儲存在資料庫中,django中可以對form定義為檔案的時候自動對上傳的檔案進行儲存的。
針對這個特性,可以利用form定義乙個檔案型別的image,把這個字段取出來實際就是記憶體中的檔案:
1、先定義乙個form專門用來對上傳檔案的處理
class uploadimageform(forms.modelform):class meta:
model = userprofile #繼承userprofile類
fields = ['image'] #對image欄位進行form處理
2、定義修改使用者頭像的view函式
class uploadimageview(loginrequiremixin,view):"""使用者修改頭像
"""def post(self, request):
image_form = uploadimageform(request.post, request.files, instance= request.user)#這裡不同的是第二個引數,之前只需要傳request.post,但對於檔案來說還需要傳檔案的上傳位址,這個引數很重要,
因為對於input和檔案上傳,檔案是放在request的不同地方的,檔案是存在request.files這個變數當中的,modelform是可以直接儲存到資料庫當中的,instance這個引數是例項化物件,傳的是modelform類裡面指定的userprofile
物件即request.user
提交頭像是通過form來的,form的這個字段 enctype="multipart/form-data" 一定要是這個型別,這樣才會把檔案型別傳遞到我們的後台
method="post" action=""這兩個引數也要這麼填
這裡的name必須是「image」,這裡很重要。
如何讀取使用者資訊
放幾個label,顯示使用者資料。protected void page load object sender,eventargs e protected void binduserinfo using system using system.data using system.configurat...
如何獲取使用者的真實IP
在jsp裡,獲取客戶端的ip位址的方法是 request.getremoteaddr 這種方法在大部分情況下都是有效的。但是在通過了 apache,nagix等反向 軟體就不能獲取到客戶端的真實ip位址了。如果使用了反向 軟體,用 request.getremoteaddr 方法獲取的ip位址是 1...
BASH 如何讀取使用者輸入
bash 指令碼如何從使用者處讀取輸入呢?1.使用read命令 2.或者 read p answer me this answer 3.或者 read pre mid post 最簡單的方式就是直接使用 read 命令,這時,會將從使用者處讀取的輸入儲存到 shell 變數reply中。如果要讓 b...