程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django18: file upload

編輯:Python

1、 Upload specification

(1) front end HTML

         File upload must be POST submission .

         expression ’<form>’ Chinese files must be uploaded with enctype=”multipart/formdata” File content data will be included when .

         Use in form <input type=”file” name=”xxx”> Tag upload file

example :
(a)views.py

# Upload files
def test_upload(request):
if request.method == 'GET':
return render(request, 'test_upload.html')
elif request.method == 'POST':
return HttpResponse(' Upload successful ')

(b)urls.py’

urlpatterns = [
path('admin/', admin.site.urls),
path('test_upload', views.test_upload),
]

(c)templates/test_upload.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Upload files </title>
</head>
<body>
<form action="/test_upload" method="post" enctype="multipart/form-data">
<p>
<input type="text" name="title">
</p>
<p>
<input type="file" name="myfile">
</p>
<p>
<input type="submit" value=" Upload ">
</p>
</form>
</body>
</html>

visit :http://192.168.28.128:8000/test_upload

 

(2) Back end Django

(a) View

         In the view function , use request.FILES Get the contents of the file box

file = request.FILES[‘XXX’]

notes :

  • FILES Of key In the corresponding page file Framed name value
  • file Bind file stream object
  • file.name file name
  • file.file Byte stream data of file

(b) The configuration file

         The access path and storage path of the configuration file : stay setting.py Set in MEDIA Related configuration ;Django The files uploaded by users are collectively referred to as media resources .

# file:settings.py
MEDIA_URL = ‘/media/’
MEDIA_ROOT = os.path.join(BASE_DIR, ‘media’)

(c) route

        MEDIA_URL and MEDIA_ROOT Manual binding required

Add a route to the main route :

from djang.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

notes : Equivalent to doing MEDIA_URL The first route ,Django After receiving the feature request, go to MEDIA_ROOT Path lookup resource .

(d) File is written to

( Method 1 ): Tradition open The way

def upload_view(request):
if request.method == 'GET':
return render(request, 'test_upload.html')
elif request.method == 'POST':
a_file = request.FILES['myfile']
print(' The upload file name is :', a_file.name)
filename = os.path.join(settings.MEDIA_ROOT, a_file.name)
with open(filename, 'wb') as f:
data = a_file.file.read()
f.write(data)
return HttpResponse(' Receive the file :' + a_file.name + ' success ')

( Method 2 ): With the help of ORM

Field :FileField(upload=’ Subdirectory name ’)

example :

(a)views.py

@csrf_exempt
def test_upload(request):
if request.method == 'GET':
return render(request, 'test_upload.html')
elif request.method == 'POST':
title = request.POST['title']
myfile = request.FILES['myfile']
Content.objects.create(title=title, picture=myfile)
return HttpResponse('-- Upload file successful --')

(b)templas/test_upload.py

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Upload files </title>
</head>
<body>
<form action="/test_upload" method="post" enctype="multipart/form-data">
{% csrf_token %}
<p>
<input type="text" name="title">
</p>
<p>
<input type="file" name="myfile">
</p>
<p>
<input type="submit" value=" Upload ">
</p>
</form>
</body>
</html>

visit :http://192.168.28.128:8000/test_upload

The picture will be saved in the project directory

In the database :

  Browser access :http://192.168.28.128:8000/media/picture/photo.jpeg

 


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved