亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術文章
文章詳情頁

Django 實現圖片上傳和下載功能

【字号: 作者:豬豬瀏覽:100日期:2024-09-14 15:32:39
原生上傳圖片方式

#新建工程 python manage.py startapp test30#修改 settings.pyINSTALLED_APPS = [ ’django.contrib.admin’, ’django.contrib.auth’, ’django.contrib.contenttypes’, ’django.contrib.sessions’, ’django.contrib.messages’, ’django.contrib.staticfiles’, ’stu’]#修改urls.pyfrom django.conf.urls import url, includefrom django.contrib import adminurlpatterns = [ url(r’^admin/’, admin.site.urls), url(r’student/’,include(’stu.urls’)),]#新增加 stu/urls.py #coding:utf-8from django.conf.urls import urlimport viewsurlpatterns = [ url(r’^$’,views.index_view)]#編輯 stu/views.py # -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.http import HttpResponsefrom django.shortcuts import render# Create your views here.#原生上傳文件方式def index_view(request): if request.method == ’GET’: return render(request,’index.html’) elif request.method == ’POST’: #獲取請求參數 uname = request.POST.get(’uname’,’’) photo = request.FILES.get(’photo’,’’) print photo.name import os print os.getcwd() if not os.path.exists(’media’): os.mkdir(’media’) #拼接路徑 with open(os.path.join(os.getcwd(),’media’,photo.name),’wb’) as fw: # photo.read() #一次性讀取文件到內存 # fw.write(photo.read()) #分塊讀取,性能高 for ck in photo.chunks():fw.write(ck) return HttpResponse(’It is post request,上傳成功’) else: return HttpResponse(’It is not post and get request!’)#新增加模板文件 templates/index.html<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/student/' method='post' enctype='multipart/form-data'> {% csrf_token %} <p> <label for='ua'>姓名: </label> <input type='text' name='uname' /> </p> <p> <label for='ph'>頭像: </label> <input type='file' name='photo' /> </p> <p> &emsp;&emsp;&emsp;&emsp;&emsp;<input type='submit' value='注冊'/> </p></form></body></html>#效果如下:訪問: http://127.0.0.1:8000/student/

Django 實現圖片上傳和下載功能

Django 實現圖片上傳和下載功能

Django 實現圖片上傳和下載功能

Django 圖片上傳方式

需求:效果: 訪問 http://127.0.0.1:8000/student/ 通過注冊將姓名、頭像地址傳入數據庫中;訪問 http://127.0.0.1:8000/student/showall 將數據庫信息通過表格形式展示###過程#修改 settings.py ,templates 新增加 ’django.template.context_processors.media’TEMPLATES = [ { ’BACKEND’: ’django.template.backends.django.DjangoTemplates’, ’DIRS’: [os.path.join(BASE_DIR, ’templates’)] , ’APP_DIRS’: True, ’OPTIONS’: { ’context_processors’: [’django.template.context_processors.debug’,’django.template.context_processors.request’,’django.contrib.auth.context_processors.auth’,’django.contrib.messages.context_processors.messages’,’django.template.context_processors.media’ ], }, },]末尾增加:# global_settings#指定上傳文件存儲相對路徑(讀取文件)MEDIA_URL = ’/media/’#指定上傳文件存儲絕對路徑(存儲文件)MEDIA_ROOT = os.path.join(BASE_DIR,’media’)#創建數據庫模型 stu/models.py# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.db import models# Create your models here.class Student(models.Model): sno = models.AutoField(primary_key=True) sname = models.CharField(max_length=30) photo = models.ImageField(upload_to=’imgs’) def __unicode__(self): return u’Student:%s’%self.sname#生成數據庫遷移文件,查看數據庫表結構python makemigrations stupython migrate #修改 urls.py 因為顯示問題,增加 DEBUG 內容from django.conf.urls import url, includefrom django.contrib import adminfrom test30.settings import DEBUG, MEDIA_ROOTurlpatterns = [ url(r’^admin/’, admin.site.urls), url(r’student/’,include(’stu.urls’)),]from django.views.static import serveif DEBUG: urlpatterns+=url(r’^media/(?P<path>.*)/$’, serve, {'document_root': MEDIA_ROOT}),#修改 urls, stu/urls.py#coding:utf-8from django.conf.urls import urlimport viewsurlpatterns = [ url(r’^$’,views.index_view), url(r’^upload/$’,views.upload_view), url(r’^showall/$’,views.showall_view)]# 修改 stu/views.py#django 上傳文件方式def upload_view(request): uname = request.POST.get(’uname’,’’) photo = request.FILES.get(’photo’,’’) #入庫操作 Student.objects.create(sname=uname,photo=photo) return HttpResponse(’上傳成功!’)#顯示圖片def showall_view(request): stus = Student.objects.all() print stus return render(request,’show.html’,{’stus’:stus})# 修改 index.html<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><form action='/student/upload/' method='post' enctype='multipart/form-data'> {% csrf_token %} <p> <label for='ua'>姓名: </label> <input type='text' name='uname' /> </p> <p> <label for='ph'>頭像: </label> <input type='file' name='photo' /> </p> <p> &emsp;&emsp;&emsp;&emsp;&emsp;<input type='submit' value='注冊'/> </p></form></body></html># 增加模板文件 show.html<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><table border='1' cellspacing='0'> <tr> <th>編號</th> <th>姓名</th> <th>頭像</th> <th>操作</th> </tr> {% for stu in stus %} <tr> <td>{{ forloop.counter }}</td> <td>{{ stu.sname }}</td> <td><img src='http://www.aoyou183.cn/bcjs/{{ MEDIA_URL }}{{ stu.photo }}'/></td> <td> 下載</td> </tr> {% endfor %}</table></body></html>效果圖:http://127.0.0.1:8000/student/ 注冊實現數據庫錄入操作(點擊提交通過index.html 中action='/student/upload/' 將url 轉發至函數upload_view ,實現上傳功能)http://127.0.0.1:8000/student/showall/ 實現數據庫信息展示

Django 實現圖片上傳和下載功能

Django 實現圖片上傳和下載功能

圖片下載功能

### 需求在顯示頁面點擊下載實現圖片的下載功能過程:#修改 show.html ,加入 下載的超鏈接<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Title</title></head><body><table border='1' cellspacing='0'> <tr> <th>編號</th> <th>姓名</th> <th>頭像</th> <th>操作</th> </tr> {% for stu in stus %} <tr> <td>{{ forloop.counter }}</td> <td>{{ stu.sname }}</td> <td><img src='http://www.aoyou183.cn/bcjs/{{ MEDIA_URL }}{{ stu.photo }}'/></td> <td><a href='http://www.aoyou183.cn/student/download/?photo={{ stu.photo }}' rel='external nofollow' >下載</a></td> </tr> {% endfor %}</table></body></html>#因為 show.html href='http://www.aoyou183.cn/student/download ,所以要修改urls#修改 stu/urls.py,新增加 urlurl(r’^download/$’,views.download_view)#修改 stu/views.pydef download_view(request): # 獲取請求參數(圖片存儲位置) imgs/5566.jpg photo = request.GET.get(’photo’,’’) print photo # 獲取圖片文件名5566.jpg ; rindex 為字符 ’/’ 在 photo 中最后出現的位置索引;例如 # txt = 'imgs/5566.jpg' # x = txt.rindex('/') # print txt[x + 1:] 輸出結果為 5566.jpg filename = photo[photo.rindex(’/’)+1:] print filename #開啟一個流 import os path = os.path.join(os.getcwd(),’media’,photo.replace(’/’,’’)) print path with open(path,’rb’) as fr: response = HttpResponse(fr.read()) response[’Content-Type’]=’image/png’ response[’Content-Disposition’] = ’attachment;filename=’ + filename return response#訪問 http://127.0.0.1:8000/student/showall/ ,點擊下載

Django 實現圖片上傳和下載功能

以上就是Django 實現圖片上傳和下載功能的詳細內容,更多關于Django 圖片上傳和下載的資料請關注好吧啦網其它相關文章!

標簽: Django
相關文章:
主站蜘蛛池模板: 久久精品国产99精品国产2021 | 婷婷在线成人免费观看搜索 | keez在线观看视频免费 | 中文字幕 亚洲 一区二区三区 | 欧美特级毛片a够爽天狼影院 | 九九小视频| 成年免费大片黄在线观看看 | 国产原创在线观看 | 欧美人妖xxx| 日韩国产欧美视频 | 一本毛片 | 国产成人精品微拍视频 | 国产精品日日摸夜夜添夜夜添1 | 成人黄色网址 | 国产一区二区免费 | 亚洲国产精品成人精品软件 | 国产精品欧美亚洲韩国日本 | 国产丝袜第一页 | 国产色中色 | 国产午夜大片 | 1000日本xxxxxxxxx25 | 国产精品爱久久电影 | 曰曰碰天天碰国产 | 国产欧美亚洲精品综合在线 | 女性被躁视频 | 日本人成免费大片 | 制服丝袜第一页在线 | 1300部小u女视频免费 | 色在线视频播放 | 欧美精品一区二区三区免费播放 | 国产成人禁片免费观看 | 亚洲丁香| 欧美成人观看 | 亚洲一级在线 | 在线观看扣喷水 | 一级aa 毛片高清免费看 | 亚洲欧美日韩一区超高清 | 亚洲精彩视频 | 国产精品视频网站你懂得 | 欧美日韩另类在线观看视频 | 国产v精品成人免费视频400条 |