django 实现将本地图片存入数据库,并能显示在web上的示例
作者:竦貊 时间:2024-01-21 14:39:33
1. 将图片存入数据库
关于数据库基本操作的学习,请参见这一篇文章:https://www.jb51.net/article/167141.htm
这里我默认,您已经会了基本操作,能在数据库中存图片了,然后,也会用图形界面操作数据库中的数据了
2.这里,我先给出我的代码,能少走些弯路就少走些
a) 项目的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)
+号后面的一定要写,如果想出来结果的话!否则回报一个 404 的错误
- b) 应用里的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
之后,你要会把IMG这个模式推送到数据库。
python ./manage.py makemigrations
python ./manage.py migrate
c) 应用的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这个参数传过去,传到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) 设置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/网址上面,上传你的图片
来源:https://blog.csdn.net/Inuyasha_1314/article/details/80531900
标签:django,图片,数据库,web
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go语言map字典用法实例分析
2024-04-26 17:29:18
PyCharm在win10的64位系统安装实例
2022-10-14 20:24:46
![](https://img.aspxhome.com/file/2023/6/109336_0s.jpg)
Sql Server 数据库索引整理语句,自动整理数据库索引
2024-01-14 02:37:25
基于PHP+Ajax实现表单验证的详解
2023-11-14 12:52:43
Python 如何读取字典的所有键-值对
2021-05-21 10:06:53
Django model序列化为json的方法示例
2022-08-25 01:39:03
浅谈Keras的Sequential与PyTorch的Sequential的区别
2021-08-24 07:53:35
Python数据可视化之matplotlib.pyplot绘图的基本参数详解
2022-06-10 12:19:48
![](https://img.aspxhome.com/file/2023/3/101153_0s.png)
OpenCV 边缘检测
2023-08-19 22:54:20
![](https://img.aspxhome.com/file/2023/0/92120_0s.jpg)
GoFrame glist 基础使用和自定义遍历
2024-05-28 15:23:26
![](https://img.aspxhome.com/file/2023/6/123886_0s.jpg)
PHP写的求多项式导数的函数代码
2023-09-10 05:42:56
python3.6、opencv安装环境搭建过程(图文教程)
2022-03-11 16:04:44
![](https://img.aspxhome.com/file/2023/2/127692_0s.jpg)
解决MySQL8.0安装第一次登陆修改密码时出现的问题
2024-01-21 16:19:49
![](https://img.aspxhome.com/file/2023/6/103546_0s.jpg)
JS中setTimeout()的用法详解
2023-06-26 20:55:17
![](https://img.aspxhome.com/file/2023/6/55876_0s.gif)
如何在小空间放置大图片
2009-08-04 13:04:00
![](https://img.aspxhome.com/file/UploadPic/20098/4/01-47s.jpg)
用实例说明python的*args和**kwargs用法
2021-05-04 08:25:55
简单form标准化实例——语义结构
2007-06-20 16:32:00
Python3爬虫带上cookie的实例代码
2021-11-10 18:06:58
MySQL创建用户和权限管理的方法
2024-01-21 12:08:58
python读取当前目录下的CSV文件数据
2022-04-06 16:50:35
![](https://img.aspxhome.com/file/2023/6/87496_0s.jpg)