欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2021年05月06日 09:16:27   作者:跟著哈哥學(xué)大智若愚  
這篇文章主要介紹了django將圖片保存到mysql數(shù)據(jù)庫并展示在前端頁面的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

小編使用python中的django框架來完成!

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

這里小編默認(rèn)項(xiàng)目都會創(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ù)庫,再輸入 create database 庫名; 一個小回車,創(chuàng)建數(shù)據(jù)庫🆗

在這里插入圖片描述

③在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/'是指定圖片存儲的文件夾名稱,上傳文件之后會自動創(chuàng)建
    img_name = models.CharField(max_length=32)
    create_time = models.DateTimeField(auto_now_add=True)

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

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

python manage.py makemigrations

python manage.py migrate

在這里插入圖片描述

3,上傳圖片功能

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/$', admin.site.urls),
    url(r'^upload/$', views.upload, name='upload'),
]

views.py

from django.shortcuts import render, redirect
from app01 import models
# Create your views here.

def upload(request):
    error = ''
    if request.method == 'POST':
        img = request.FILES.get('img')
        pic_name = img.name
        if 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())

前端上傳頁面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>上傳圖片頁面</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=" {% url 'show' %} " rel="external nofollow" >返回</a>
</div>
</body>
</html>

在這里插入圖片描述

4,展示圖片功能

urls.py

"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    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, redirect
from app01 import models


# Create your views here.


def upload(request):
    error = ''
    if request.method == 'POST':
        img = request.FILES.get('img')
        pic_name = img.name
        if 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="/{{ image.img }}" style="width: 240px;height: 240px;">
{% endfor %}
<br/>
<p style="text-align: center;color: #2b542c;font-size: 20px;">
    <a href="{% url 'upload' %}" rel="external nofollow"  rel="external nofollow" >返回</a>
</p>
</body>
</html>

在這里插入圖片描述

5,刪除圖片功能

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    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, redirect
from app01 import models


# Create your views here.


def upload(request):
    error = ''
    if request.method == 'POST':
        img = request.FILES.get('img')
        pic_name = img.name
        if 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="/{{ image.img }}" style="width: 240px;height: 240px;">
    <a href="/delete/?pk={{ image.id }}" rel="external nofollow" >刪除</a>
{% endfor %}
<br/>
<p style="text-align: center;color: #2b542c;font-size: 20px;">
    <a href="{% url 'upload' %}" rel="external nofollow"  rel="external nofollow" >返回</a>
</p>
</body>
</html>

在這里插入圖片描述

6,整體演示一遍

在這里插入圖片描述

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

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

相關(guān)文章

  • Python實(shí)現(xiàn)的當(dāng)前時間多加一天、一小時、一分鐘操作示例

    Python實(shí)現(xiàn)的當(dāng)前時間多加一天、一小時、一分鐘操作示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的當(dāng)前時間多加一天、一小時、一分鐘操作,結(jié)合實(shí)例形式分析了Python基于datetime模塊進(jìn)行日期時間操作相關(guān)使用技巧,需要的朋友可以參考下
    2018-05-05
  • Python包裝異常處理方法

    Python包裝異常處理方法

    這篇文章主要介紹了Python包裝異常處理方法,相比java,python的異常和java中不同,python主要是防止程序異常被中止。一旦被catch后它還行往下執(zhí)行,本文就分享python相關(guān)的異常處理方法,需要的小伙伴可以參考一下
    2022-06-06
  • Linux環(huán)境下MySQL-python安裝過程分享

    Linux環(huán)境下MySQL-python安裝過程分享

    這篇文章主要介紹了Linux環(huán)境下MySQL-python安裝過程分享,本文使用的編譯方式安裝,需要的朋友可以參考下
    2015-02-02
  • 詳解如何理解并正確使用Python中的f字符串

    詳解如何理解并正確使用Python中的f字符串

    Python中的f字符串是一種字符串格式化語法,它可以將變量、表達(dá)式和函數(shù)等動態(tài)地嵌入到字符串中,本文就來詳細(xì)講講如何理解并正確使用它吧
    2023-06-06
  • 用Python實(shí)現(xiàn)協(xié)同過濾的教程

    用Python實(shí)現(xiàn)協(xié)同過濾的教程

    這篇文章主要介紹了用Python實(shí)現(xiàn)協(xié)同過濾的教程,主要用于從大數(shù)據(jù)中抽取用戶信息偏好等等,需要的朋友可以參考下
    2015-04-04
  • Python numpy中矩陣的基本用法匯總

    Python numpy中矩陣的基本用法匯總

    這篇文章主要給大家介紹了關(guān)于Python numpy中矩陣的基本用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • 使用python批量生成insert語句的方法

    使用python批量生成insert語句的方法

    很多時候需要造數(shù)據(jù),大量的插入數(shù)據(jù),本文介紹了使用python批量生成insert語句的方法,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Python中的np.argmin()和np.argmax()函數(shù)用法

    Python中的np.argmin()和np.argmax()函數(shù)用法

    這篇文章主要介紹了Python中的np.argmin()和np.argmax()函數(shù)用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Pandas時間序列:時期(period)及其算術(shù)運(yùn)算詳解

    Pandas時間序列:時期(period)及其算術(shù)運(yùn)算詳解

    今天小編就為大家分享一篇Pandas時間序列:時期(period)及其算術(shù)運(yùn)算詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法分析

    Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法分析

    這篇文章主要介紹了Python面向?qū)ο笾o態(tài)屬性、類方法與靜態(tài)方法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中靜態(tài)屬性、類方法及靜態(tài)方法相關(guān)概念、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2018-08-08

最新評論