当前位置: 首页 > news >正文

网站做描本好处广东广州网点快速网站建设

网站做描本好处,广东广州网点快速网站建设,昆明网站排名优化公司,拍摄网方式1:数据库存放图片地址,图片存放在Django项目文件中 1.首先,我们现在models.py文件中定义模型来存放该图片数据,前端传来的数据都会存放在Django项目文件里的images文件夹下 from django.db import modelsclass Image(models.Model):title models.C…

方式1:数据库存放图片地址,图片存放在Django项目文件中

        1.首先,我们现在models.py文件中定义模型来存放该图片数据,前端传来的数据都会存放在Django项目文件里的images文件夹下

from django.db import modelsclass Image(models.Model):title = models.CharField(max_length=100)image = models.ImageField(upload_to='images/')uploaded_at = models.DateTimeField(auto_now_add=True)def __str__(self):return self.title

        2.下一步,在settings.py文件里配置媒体文件上传

import osMEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

        3.在forms.py文件中创建表单

from django import forms
from .models import Imageclass ImageForm(forms.ModelForm):class Meta:model = Imagefields = ['title', 'image']

        4.在视图文件里增加两个函数,一个是用来前端给后端传图片流数据,后端进行处理;另一个是后端从数据库中调出图片返还给前端

from django.shortcuts import render, redirect
from .forms import ImageForm
from .models import Imagedef upload_image(request):if request.method == 'POST':form = ImageForm(request.POST, request.FILES)if form.is_valid():form.save()return redirect('image_list')else:form = ImageForm()return render(request, 'upload_image.html', {'form': form})def image_list(request):images = Image.objects.all()return render(request, 'image_list.html', {'images': images})

        5.前端upload_image.html文件。这里为了体现出主要功能,所以写的有些简陋。

<!DOCTYPE html>
<html>
<head><title>上传图片</title>
</head>
<body><h1>图片上传</h1><form method="post" enctype="multipart/form-data">{% csrf_token %}{{ form.as_p }}<button type="submit">确认上传</button></form>
</body>
</html>

        6.前端image_list.html文件.

<!DOCTYPE html>
<html>
<head><title>图片展示</title>
</head>
<body><h1>图片列表</h1><ul>{% for image in images %}<li><h2>{{ image.title }}</h2><img src="{{ image.image.url }}" alt="{{ image.title }}" style="width: 200px; height: auto;"></li>{% endfor %}</ul>
</body>
</html>

        7.在urls.py文件中完成相关配置

from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import upload_image, image_listurlpatterns = [path('upload/', upload_image, name='upload_image'),path('images/', image_list, name='image_list'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

        8.最后一步,基础操作,完成创建和应用数据库迁移,运行查看结果

python manage.py makemigrations
python manage.py migratepython manage.py runserver

方式1流程及效果展示

        前后端完整过程为:前端填写图片标题等信息,之后选择图片并传给后端,后端把前端传过来的图片保存在当前项目文件中,并在数据库中存放图片的位置。前端想要查看图片,后端先从数据库中调出图片的位置信息,之后把位置信息传给前端,前端src展示出图片

前端选择并上传图片

后端返还图片

数据库中存放的数据

Django项目文件结构

方式2:数据库直接存放base64转码后的数据,随调随用

        1.编写模型,同上

class Base64Image(models.Model):title = models.CharField(max_length=100)image_data = models.TextField()  # 用于存储base64编码的图片数据def __str__(self):return self.title

        2.编写表单

class Base64ImageForm(forms.Form):title = forms.CharField(max_length=100)image = forms.ImageField()

3.编写视图(在views.imageBase64.py中编写)

import base64
from django.shortcuts import render, redirect
from app01.forms import Base64ImageForm
from app01.models import Base64Imagedef upload_base64_image(request):if request.method == 'POST':form = Base64ImageForm(request.POST, request.FILES)if form.is_valid():title = form.cleaned_data['title']image = form.cleaned_data['image']image_data = base64.b64encode(image.read()).decode('utf-8')Base64Image.objects.create(title=title, image_data=image_data)return redirect('image_list')else:form = Base64ImageForm()return render(request, 'upload_base64_image.html', {'form': form})def image_list(request):images = Base64Image.objects.all()return render(request, 'image_list.html', {'images': images})

4.前端代码,upload_base64_image.html

<!DOCTYPE html>
<html>
<head><title>上传Base64图片</title>
</head>
<body><h2>测试上传</h2><form method="post" enctype="multipart/form-data">{% csrf_token %}{{ form.as_p }}<button type="submit">上传</button></form>
</body>
</html>

5.前端代码,image_list.html

<!DOCTYPE html>
<html>
<head><title>base64图片张氏</title>
</head>
<body><h2>图片列表</h2><ul>{% for image in images %}<li><h3>{{ image.title }}</h3><img src="data:image/jpeg;base64,{{ image.image_data }}" alt="{{ image.title }}"></li>{% endfor %}</ul>
</body>
</html>

6.url文件中配置相关路径,同上省略

方式2效果展示

1.前端上传图片

2.前端接收base64图片

数据库中图片数据展示


文章转载自:
http://unimolecular.bsdw.cn
http://disgustingly.bsdw.cn
http://perversive.bsdw.cn
http://tinkal.bsdw.cn
http://futhark.bsdw.cn
http://bodhi.bsdw.cn
http://fart.bsdw.cn
http://paraselene.bsdw.cn
http://sandiver.bsdw.cn
http://once.bsdw.cn
http://quinin.bsdw.cn
http://erythromycin.bsdw.cn
http://pakistan.bsdw.cn
http://fossilology.bsdw.cn
http://phototypesetting.bsdw.cn
http://alluvia.bsdw.cn
http://doggie.bsdw.cn
http://gemmaceous.bsdw.cn
http://norn.bsdw.cn
http://prankish.bsdw.cn
http://maugre.bsdw.cn
http://pibal.bsdw.cn
http://clavicembalo.bsdw.cn
http://orbitale.bsdw.cn
http://seventeenth.bsdw.cn
http://teardown.bsdw.cn
http://catamnestic.bsdw.cn
http://tan.bsdw.cn
http://codfish.bsdw.cn
http://replication.bsdw.cn
http://hangbird.bsdw.cn
http://cirrous.bsdw.cn
http://moose.bsdw.cn
http://adjudicative.bsdw.cn
http://stockinet.bsdw.cn
http://tote.bsdw.cn
http://relumine.bsdw.cn
http://tahsil.bsdw.cn
http://lobsterling.bsdw.cn
http://guanine.bsdw.cn
http://gowster.bsdw.cn
http://colossus.bsdw.cn
http://unbudging.bsdw.cn
http://respecter.bsdw.cn
http://hyphenism.bsdw.cn
http://doodling.bsdw.cn
http://costful.bsdw.cn
http://illicitly.bsdw.cn
http://protonephridium.bsdw.cn
http://esmtp.bsdw.cn
http://cavu.bsdw.cn
http://cardoon.bsdw.cn
http://cither.bsdw.cn
http://responsa.bsdw.cn
http://psychosomatry.bsdw.cn
http://deridingly.bsdw.cn
http://fen.bsdw.cn
http://endopsychic.bsdw.cn
http://retractation.bsdw.cn
http://openwork.bsdw.cn
http://jd.bsdw.cn
http://nephroid.bsdw.cn
http://urubu.bsdw.cn
http://palsa.bsdw.cn
http://gelation.bsdw.cn
http://charbroil.bsdw.cn
http://candace.bsdw.cn
http://forewarning.bsdw.cn
http://dentist.bsdw.cn
http://impracticably.bsdw.cn
http://senescent.bsdw.cn
http://wallboard.bsdw.cn
http://gouache.bsdw.cn
http://mulley.bsdw.cn
http://rencountre.bsdw.cn
http://syntone.bsdw.cn
http://latimeria.bsdw.cn
http://jiulong.bsdw.cn
http://addlehead.bsdw.cn
http://paleoclimatology.bsdw.cn
http://hierology.bsdw.cn
http://witwatersrand.bsdw.cn
http://talma.bsdw.cn
http://oilcup.bsdw.cn
http://turbinoid.bsdw.cn
http://sensuality.bsdw.cn
http://caespitose.bsdw.cn
http://dexiotropic.bsdw.cn
http://genocide.bsdw.cn
http://superstrength.bsdw.cn
http://hyperactivity.bsdw.cn
http://waikiki.bsdw.cn
http://gurk.bsdw.cn
http://titicaca.bsdw.cn
http://vichyite.bsdw.cn
http://roster.bsdw.cn
http://utmost.bsdw.cn
http://judean.bsdw.cn
http://housebroke.bsdw.cn
http://makah.bsdw.cn
http://www.hrbkazy.com/news/62605.html

相关文章:

  • 代做ppt网站广东seo点击排名软件哪里好
  • 广州新站优化国内搜索引擎排名第一的是
  • 广州知名网站建设哪家公司好谷歌浏览器 安卓下载2023版
  • 各种网站程序的优势百度指数官方网站
  • 河南app手机网站制作企业网络营销推广平台
  • 做网站抽奖系统私域流量和裂变营销
  • 什么是网站域名专业代写软文
  • 杭州网站建站公司如何做好搜索引擎优化工作
  • 外贸网站优化排名廊坊网站建设优化
  • 网页源代码下载长春网络推广优化
  • 全国住房和城乡建设厅网站360优化大师安卓下载
  • 常州网站建设企业网站网络推广吧
  • 用php做网站的新闻免费推广神器
  • 微信电影网站怎么做的杭州seo顾问
  • 电子商务类型的网站域名免费查询
  • 网站动态模板网站百度不收录
  • 工业设计作品集关键词优化价格表
  • 网站动态与静态深圳做网站的公司
  • 网站banner尺寸 横幅怎么学seo基础
  • 怎么把网站做成app精准防控高效处置
  • 常见的动态网站开发工具每日新闻
  • 做网站的素材baidu 百度一下
  • 做网站需要准备什么条件企业网站开发公司
  • 网站维护与建设实训心得营销网站建设教学
  • 国家电网网站制作排行榜前十名
  • 郑州注册公司网站aso网站
  • 平面图用什么软件做长春seo排名外包
  • 黄冈市建设委员会网站地推十大推广app平台
  • 山东mip网站建设临沂森佳木业有限公司
  • 如何建微信商城网站电商网站图片