django 實(shí)現(xiàn)將本地圖片存入數(shù)據(jù)庫,并能顯示在web上的示例
1. 將圖片存入數(shù)據(jù)庫
關(guān)于數(shù)據(jù)庫基本操作的學(xué)習(xí),請參見這一篇文章:http://www.dbjr.com.cn/article/167141.htm
這里我默認(rèn),您已經(jīng)會(huì)了基本操作,能在數(shù)據(jù)庫中存圖片了,然后,也會(huì)用圖形界面操作數(shù)據(jù)庫中的數(shù)據(jù)了
2.這里,我先給出我的代碼,能少走些彎路就少走些
a) 項(xiàng)目的urls.py
from django.contrib import admin from django.urls import path from django.conf import settings from django.conf.urls.static import static urlpatterns = [ path('admin/', admin.site.urls), ]+static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
+號后面的一定要寫,如果想出來結(jié)果的話!否則回報(bào)一個(gè) 404 的錯(cuò)誤
- b) 應(yīng)用里的models.py
from django.db import models # Create your models here. class Person(models.Model): name = models.CharField(max_length=30) age = models.IntegerField() def __unicode__(self): # 在Python3中使用 def __str__(self): return self.name class IMG(models.Model): img = models.ImageField(upload_to='img') name = models.CharField(max_length=20) def __str__(self): # 在Python3中使用 def __str__(self): return self.name
之后,你要會(huì)把IMG這個(gè)模式推送到數(shù)據(jù)庫。
python ./manage.py makemigrations python ./manage.py migrate
c) 應(yīng)用的views.py
# Create your views here. def hello(request): IMG.objects.filter(name='bg') img = IMG.objects.all() return render(request, 'Welcome.html',{'img':img})
把img這個(gè)參數(shù)傳過去,傳到Welcome.html
- d) Welcome.html
<!DOCTYPE HTML> <html> <head> <title> welcome </title> </head> <body > {% for i in img %} <img src="{{MEDIA_URL}}{{i.img}}"> {% endfor %} </body> </html>
e) 設(shè)置setting.py
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], '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', ], }, }, ] MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
注意,東西都是配套使用的,如果e中的路徑要變的話,a總的+號后面的也要跟著變化
3. 在http://127.0.0.1:8000/admin/網(wǎng)址上面,上傳你的圖片
以上這篇django 實(shí)現(xiàn)將本地圖片存入數(shù)據(jù)庫,并能顯示在web上的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
淺談python 中的 type(), dtype(), astype()的區(qū)別
這篇文章主要介紹了淺談python 中的 type(), dtype(), astype()的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04numpy的Fancy Indexing和array比較詳解
這篇文章主要介紹了numpy的Fancy Indexing和array比較詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Pycharm同步遠(yuǎn)程服務(wù)器調(diào)試的方法步驟
這篇文章主要介紹了Pycharm同步遠(yuǎn)程服務(wù)器調(diào)試,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11NumPy實(shí)現(xiàn)ndarray多維數(shù)組操作
NumPy一個(gè)非常重要的作用就是可以進(jìn)行多維數(shù)組的操作,這篇文章主要介紹了NumPy實(shí)現(xiàn)ndarray多維數(shù)組操作,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05NumPy實(shí)現(xiàn)多維數(shù)組中的線性代數(shù)
本文主要介紹了NumPy實(shí)現(xiàn)多維數(shù)組中的線性代數(shù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07Python深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)基本原理
人工神經(jīng)網(wǎng)絡(luò)(Artificial Neural Networks,簡寫為ANNs)也簡稱為神經(jīng)網(wǎng)絡(luò)(NNs)或稱作連接模型(Connection Model),它是一種模仿動(dòng)物神經(jīng)網(wǎng)絡(luò)行為特征,進(jìn)行分布式并行信息處理的算法數(shù)學(xué)模型2021-10-10如何利用Python統(tǒng)計(jì)正數(shù)和負(fù)數(shù)的個(gè)數(shù)
Python檢查數(shù)據(jù)中的正/負(fù)數(shù)是一種常見的數(shù)據(jù)處理操作,可以通過編寫代碼來實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于如何利用Python統(tǒng)計(jì)正數(shù)和負(fù)數(shù)的個(gè)數(shù)的相關(guān)資料,需要的朋友可以參考下2024-05-05