django将图⽚保存到mysql数据库并展⽰在前端页⾯的实现⼩编使⽤python中的django框架来完成!
1,⾸先⽤pycharm创建django项⽬并配置相关环境
这⾥⼩编默认项⽬都会创建
settings.py中要修改的两处配置
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
'ENGINE': 'django.sql',
'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,创建表
①先按键盘上win+s键,然后输⼊cmd,中⽂输⼊法两下回车,英⽂输⼊法⼀下回车,即可进⼊dos窗⼝。
②输⼊ mysql -uroot -p密码回车进⼊mysql数据库,再输⼊ create database 库名; ⼀个⼩回车,创建数据库
③在app下的models.py中创建表结构
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/'是指定图⽚存储的⽂件夹名称,上传⽂件之后会⾃动创建
img_name = models.CharField(max_length=32)
create_time = models.DateTimeField(auto_now_add=True)
④迁移数据库
分别按顺序在pycharm下⾯的Terminal中执⾏下⾯两条语句
python manage.py makemigrations
python manage.py migrate
3,上传图⽚功能
urls.py
f.urls import url
ib 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 = ''
hod == 'POST':
img = ('img')
pic_name = img.name
if pic_name.split('.')[-1] == 'mp4':
error = '暂不⽀持上传此格式图⽚'
else:
models.ate(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 >
<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 >{{ error }}</strong></td>
</tr>
</table>
</form>
</div>
<div >
<a href=" {% url 'show' %} " rel="external nofollow" >返回</a>
</div>
</body>
</html>
4,展⽰图⽚功能
urls.py
"""
f.urls import url
ib 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 = ''
hod == 'POST':
img = ('img')
pic_name = img.name
if pic_name.split('.')[-1] == 'mp4':
error = '暂不⽀持上传此格式图⽚'
else:
models.ate(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 }}" >
{% endfor %}
<br/>
<p >
<a href="{% url 'upload' %}" rel="external nofollow"  rel="external nofollow" >返回</a> </p>
</body>
</html>
5,删除图⽚功能
urls.py
f.urls import url
ib 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 = ''
hod == 'POST':
img = ('img')
pic_name = img.name
if pic_name.split('.')[-1] == 'mp4':
error = '暂不⽀持上传此格式图⽚'
else:
models.ate(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 = ('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 }}" >
<a href="/delete/?pk={{ image.id }}" rel="external nofollow" >删除</a>
{% endfor %}
<br/>
<p >
<a href="{% url 'upload' %}" rel="external nofollow"  rel="external nofollow" >返回</a> </p>
</body>
django admin 自定义页面
</html>
6,整体演⽰⼀遍
因为时间紧,故以最low⽅式简要实现,并没有加上漂亮的页⾯和样式,喜欢美的看客朋友可⾃⾏去Bootstrap官⽹或jq22⾃⾏添加
到此这篇关于django将图⽚保存到mysql数据库并展⽰在前端页⾯的实现的⽂章就介绍到这了,更多相关django 图⽚保存到mysql内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。