一、新建項目,在主配置文件中,修改以下內(nèi)容:
1
2
3
|
ALLOWED_HOSTS = [ '127.0.0.1' , 'localhost' ] MEDIA_ROOT = os.path.join(BASE_DIR, 'media' ) STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static' ), MEDIA_ROOT] |
在該項目下新建一個與 manage.py 同級的目錄文件,目錄名為media。
在media文件夾下新建一個子目錄,作為上傳文件的保存位置,這里我把該子目錄命名為headpics。即模擬保存用戶選擇的頭像文件。
二、新建APP(這里我把該app命名為uploadFile)
執(zhí)行如下命令將創(chuàng)建app:
python manage.py startapp uploadFile
在uploadFile下的models.py文件下,粘貼如下代碼:
1
2
3
4
5
|
from django.db import models class User(models.Model): name = models.CharField(max_length = 12 ) file = models.FileField(upload_to = 'headpics' ) |
不要忘了在主配置文件的 INSTALLED_APPS 列表下注冊該app。
然后在控制臺執(zhí)行如下命令:
1
2
|
python manage.py makemigrations uploadFile python manage.py migrate uploadFile |
三、編寫路由
在主路由模塊下粘貼如下代碼:
1
2
3
4
5
6
7
|
from django.contrib import admin from django.urls import path,include urlpatterns = [ path( 'admin/' , admin.site.urls), path( 'index/' ,include( "uploadFile.urls" )), ] |
admin 是pycharm自動添加的,不需要的話可以刪去。
然后在uploadFile下新建urls.py模塊,即編寫二級路由。
在二級路由下粘貼如下代碼:
1
2
3
4
5
6
7
8
|
from django.urls import path from . import views app_name = 'upload' urlpatterns = [ path( 'file/' ,views.userfile,name = 'userfile' ), path( 'file/detail/' ,views.detailFile,name = 'delfile' ), ] |
四、編寫視圖函數(shù)
在uploadFile的views.py文件下粘貼如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
from django.shortcuts import render,get_object_or_404 from django.http import HttpResponse import uuid,os from .models import User # Create your views here. def userfile(request): return render(request, 'uploadFile/uploadFile.html' ) def detailFile(request): if request.method = = "POST" : name = request.POST.get( 'name' ) file = request.FILES.get( 'file' , None ) if not file : return HttpResponse( "<p>您還未上傳頭像!</p>" ) file .name = getUUID( file .name) user = User.objects.create(name = name, file = file ) with open (os.path.join( "D:\\upload" , file .name), 'wb+' ) as relfile: for crunk in file .chunks(): relfile.write(crunk) return HttpResponse( "<p>上傳成功!</p>" ) else : pass def getUUID(filename): id = str (uuid.uuid4()) extend = os.path.splitext(filename)[ 1 ] return id + extend |
五、編寫模板
在uploadFile下新建一個叫做templates的目錄,在該目錄下再新建一個叫做uploadFile的子目錄,在該子目錄下再新建一個叫做uploadFile的html文件。
在該html文件內(nèi)粘貼如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!DOCTYPE html> <html lang = "en" > <head> <meta charset = "UTF-8" > <title>upload< / title> < / head> <body> <form action = "{% url 'upload:delfile' %}" method = "post" enctype = "multipart/form-data" > { % csrf_token % } 昵稱 :< input type = "text" name = "name" ><br><br> 頭像 : < input type = "file" name = "file" ><br><br> < input type = "submit" value = "提交" > < / form> < / body> < / html> |
大功告成!
運行之后,在瀏覽器 輸入 http://127.0.0.1:8000/index/file/ 可以看到界面效果,如下:
輸入之后點擊提交,數(shù)據(jù)就會被實時保存在數(shù)據(jù)庫中,不過要記得在數(shù)據(jù)庫中 file 字段保存的其實是文件的路徑信息,是一個字符串。
同時,該文件也會保存在剛才創(chuàng)建的upload文件夾下。
可以嘗試添加更多內(nèi)容,比如在提交成功之后返回一個效果頁面等。
總結(jié)
以上所述是小編給大家介紹的django 文件上傳功能的相關(guān)實例代碼,希望對大家有所幫助!
原文鏈接:https://blog.csdn.net/ckk727/article/details/104062238