django框架中如何存图片到mysql,方法是什么
Admin 2022-06-16 群英技术资讯 425 次浏览
小编使用python中的django框架来完成!
这里小编默认项目都会创建
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') ]
①先按键盘上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
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>
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>
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>
因为时间紧,故以最low方式简要实现,并没有加上漂亮的页面和样式,喜欢美的看客朋友可自行去Bootstrap官网或jq22自行添加!!!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
文件操作的步骤:打开文件 -> 操作文件 -> 关闭文件切记:最后要关闭文件(否则可能会有意想不到的结果)打开文件文件句柄 = open(&
这篇文章主要介绍了python重复值处理得方法,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
这篇文章主要为大家介绍了python虚拟机解释器及运行过程的介绍,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
@用做函数的修饰符,可以在模块或者类的定义层内对函数进行修饰,下面这篇文章主要给大家介绍了关于python中 @ 的含义以及基本使用的相关资料,需要的朋友可以参考下
缓存是一种优化技术,可以在应用程序中使用它来将最近或经常使用的数据保存在内存中,通过这种方式来访问数据的速度比直接读取磁盘文件的高很多
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008