国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

瀏覽:16日期:2023-10-18 09:26:51

小編使用python中的django框架來(lái)完成!

1,首先用pycharm創(chuàng)建django項(xiàng)目并配置相關(guān)環(huán)境

這里小編默認(rèn)項(xiàng)目都會(huì)創(chuàng)建

settings.py中要修改的兩處配置

DATABASES = { ’default’: {# ’ENGINE’: ’django.db.backends.sqlite3’,# ’NAME’: os.path.join(BASE_DIR, ’db.sqlite3’),’ENGINE’: ’django.db.backends.mysql’,’NAME’: ’photos’,’HOST’: ’127.0.0.1’,’PORT’: ’3306’,’USER’: ’root’,’PASSWORD’: ’201314’, }}STATIC_URL = ’/static/’STATICFILES_DIRS = [ os.path.join(BASE_DIR, ’static’)]2,創(chuàng)建表

①先按鍵盤上win+s鍵,然后輸入cmd,中文輸入法兩下回車,英文輸入法一下回車,即可進(jìn)入dos窗口。

②輸入 mysql -uroot -p密碼 回車進(jìn)入mysql數(shù)據(jù)庫(kù),再輸入 create database 庫(kù)名; 一個(gè)小回車,創(chuàng)建數(shù)據(jù)庫(kù)🆗

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

③在app下的models.py中創(chuàng)建表結(jié)構(gòu)

models.py

from django.db import models# Create your models here.class Images(models.Model): img = models.ImageField(upload_to=’static/pictures/’) # upload_to=’static/pictures/’是指定圖片存儲(chǔ)的文件夾名稱,上傳文件之后會(huì)自動(dòng)創(chuàng)建 img_name = models.CharField(max_length=32) create_time = models.DateTimeField(auto_now_add=True)

④遷移數(shù)據(jù)庫(kù)

分別按順序在pycharm下面的Terminal中執(zhí)行下面兩條語(yǔ)句

python manage.py makemigrationspython manage.py migrate

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

3,上傳圖片功能

urls.py

from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r’^admin/$’, admin.site.urls), url(r’^upload/$’, views.upload, name=’upload’),]

views.py

from django.shortcuts import render, redirectfrom app01 import models# Create your views here.def upload(request): error = ’’ if request.method == ’POST’:img = request.FILES.get(’img’)pic_name = img.nameif pic_name.split(’.’)[-1] == ’mp4’: error = ’暫不支持上傳此格式圖片!!!’else: models.Images.objects.create(img_name=pic_name, img=img) return redirect(’show’) return render(request, ’upload.html’, locals())

前端上傳頁(yè)面upload.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>上傳照片</title></head><body><div style='height: 160px'> <form action='' method='post' enctype='multipart/form-data'>{% csrf_token %}<h1>上傳圖片頁(yè)面</h1><table cellpadding='5px'> <tr><td>上傳圖片</td><td><input type='file' name='img'></td> </tr> <tr><td> <button>上傳</button></td><td><strong style='color: red'>{{ error }}</strong></td> </tr></table> </form></div><div style='text-align: center;color: #2b542c;font-size: 20px;'> <a href='http://m.cgvv.com.cn/bcjs/ {% url ’show’ %} ' rel='external nofollow' >返回</a></div></body></html>

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

4,展示圖片功能

urls.py

'''from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r’^admin/$’, admin.site.urls), url(r’^upload/$’, views.upload, name=’upload’), url(r’^show/$’, views.show, name=’show’),]

views.py

from django.shortcuts import render, redirectfrom app01 import models# Create your views here.def upload(request): error = ’’ if request.method == ’POST’:img = request.FILES.get(’img’)pic_name = img.nameif pic_name.split(’.’)[-1] == ’mp4’: error = ’暫不支持上傳此格式圖片!!!’else: models.Images.objects.create(img_name=pic_name, img=img) return redirect(’show’) return render(request, ’upload.html’, locals())def show(request): all_images = models.Images.objects.all() # for i in all_images: # print(i.img) return render(request, ’show.html’, locals())

前端展示show.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>展示照片</title></head><body>{% for image in all_images %} <img src='https://rkxy.com.cn/{{ image.img }}' style='width: 240px;height: 240px;'>{% endfor %}<br/><p style='text-align: center;color: #2b542c;font-size: 20px;'> <a href='http://m.cgvv.com.cn/bcjs/{% url ’upload’ %}' rel='external nofollow' rel='external nofollow' >返回</a></p></body></html>

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

5,刪除圖片功能

urls.py

from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [ url(r’^admin/$’, admin.site.urls), url(r’^upload/$’, views.upload, name=’upload’), url(r’^show/$’, views.show, name=’show’), url(r’^delete/$’, views.delete, name=’delete’),]

views.py

from django.shortcuts import render, redirectfrom app01 import models# Create your views here.def upload(request): error = ’’ if request.method == ’POST’:img = request.FILES.get(’img’)pic_name = img.nameif pic_name.split(’.’)[-1] == ’mp4’: error = ’暫不支持上傳此格式圖片!!!’else: models.Images.objects.create(img_name=pic_name, img=img) return redirect(’show’) return render(request, ’upload.html’, locals())def show(request): all_images = models.Images.objects.all() # for i in all_images: # print(i.img) return render(request, ’show.html’, locals())def delete(request): pk = request.GET.get(’pk’) models.Images.objects.filter(id=pk).delete() return redirect(’show’)

show.html

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>展示照片</title></head><body>{% for image in all_images %} <img src='https://rkxy.com.cn/{{ image.img }}' style='width: 240px;height: 240px;'> <a href='http://m.cgvv.com.cn/delete/?pk={{ image.id }}' rel='external nofollow' >刪除</a>{% endfor %}<br/><p style='text-align: center;color: #2b542c;font-size: 20px;'> <a href='http://m.cgvv.com.cn/bcjs/{% url ’upload’ %}' rel='external nofollow' rel='external nofollow' >返回</a></p></body></html>

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

6,整體演示一遍

django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)

因?yàn)闀r(shí)間緊,故以最low方式簡(jiǎn)要實(shí)現(xiàn),并沒(méi)有加上漂亮的頁(yè)面和樣式,喜歡美的看客朋友可自行去Bootstrap官網(wǎng)或jq22自行添加!!!

到此這篇關(guān)于django將圖片保存到mysql數(shù)據(jù)庫(kù)并展示在前端頁(yè)面的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)django 圖片保存到mysql內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: MySQL 數(shù)據(jù)庫(kù)
相關(guān)文章:
主站蜘蛛池模板: 完全免费在线视频 | 国产精品无码久久久久 | 欧美日韩第三页 | 欧美另类视频一区二区三区 | 欧美激情第一欧美在线 | 毛片一级做a爰片性色 | 亚洲影视一区二区 | 91专区在线| 男人亚洲天堂 | 扒开双腿猛进入爽爽在线观看 | 午夜香蕉成视频人网站高清版 | 欧美成人亚洲综合精品欧美激情 | 欧洲一级大片 | 在线精品欧美日韩 | 日本不卡免费高清一级视频 | 国产日产韩产麻豆1区 | 国产成人免费高清视频网址 | 久久精品国产这里是免费 | 久久久久综合给合狠狠狠 | 欧美国产日韩在线 | 香港三级日本三级三级人妇 | 亚洲精品亚洲人成毛片不卡 | 岛国大片在线播放高清 | 99av在线 | 成人a毛片久久免费播放 | 欧美精品99 | 日韩a一级欧美一级 | 欧美第一页草草影院浮力 | 国产日本在线 | 亚洲网站免费看 | 久久九九精品一区二区 | 国产成人91高清精品免费 | 欧美日韩国产在线人成dvd | 黄色片日本人 | 国产欧美成人免费观看视频 | 国产亚洲自在精品久久 | 成人免费在线播放视频 | 国产欧美日韩精品在线 | 国产美女操 | 日韩一级在线播放免费观看 | 亚洲欧美在线综合一区二区三区 |