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

网站做描本好处什么是seo关键词

网站做描本好处,什么是seo关键词,物联网应用有哪些,网站素材网方式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://pharmacopsychosis.c7507.cn
http://norethynodrel.c7507.cn
http://hackmanite.c7507.cn
http://pronounceable.c7507.cn
http://syndeton.c7507.cn
http://slowgoing.c7507.cn
http://brett.c7507.cn
http://firebrat.c7507.cn
http://photolithograph.c7507.cn
http://twoness.c7507.cn
http://exhume.c7507.cn
http://methanogen.c7507.cn
http://bedchamber.c7507.cn
http://cogitable.c7507.cn
http://braceleted.c7507.cn
http://perfusate.c7507.cn
http://extrarenal.c7507.cn
http://tantra.c7507.cn
http://segment.c7507.cn
http://nymphal.c7507.cn
http://knock.c7507.cn
http://wold.c7507.cn
http://samekh.c7507.cn
http://mustardy.c7507.cn
http://mulattress.c7507.cn
http://abortifacient.c7507.cn
http://less.c7507.cn
http://annul.c7507.cn
http://wesley.c7507.cn
http://capitula.c7507.cn
http://stravinskian.c7507.cn
http://molasses.c7507.cn
http://uranism.c7507.cn
http://monistic.c7507.cn
http://keratoma.c7507.cn
http://haka.c7507.cn
http://zoografting.c7507.cn
http://iula.c7507.cn
http://totalling.c7507.cn
http://gastrojejunostomy.c7507.cn
http://quietness.c7507.cn
http://practise.c7507.cn
http://whys.c7507.cn
http://nozzle.c7507.cn
http://cruet.c7507.cn
http://granitite.c7507.cn
http://cadaster.c7507.cn
http://rowdedowdy.c7507.cn
http://randomly.c7507.cn
http://shopman.c7507.cn
http://patio.c7507.cn
http://whitefly.c7507.cn
http://solatia.c7507.cn
http://hindostan.c7507.cn
http://saurel.c7507.cn
http://tabourine.c7507.cn
http://brokage.c7507.cn
http://glancing.c7507.cn
http://rescuee.c7507.cn
http://transcendency.c7507.cn
http://biographee.c7507.cn
http://petulance.c7507.cn
http://nobleness.c7507.cn
http://endotherm.c7507.cn
http://menage.c7507.cn
http://environal.c7507.cn
http://zach.c7507.cn
http://decolorant.c7507.cn
http://rebeldom.c7507.cn
http://baghdad.c7507.cn
http://stakeholder.c7507.cn
http://entebbe.c7507.cn
http://denim.c7507.cn
http://craps.c7507.cn
http://chlorella.c7507.cn
http://experienced.c7507.cn
http://cornichon.c7507.cn
http://wayang.c7507.cn
http://as.c7507.cn
http://abstracted.c7507.cn
http://examine.c7507.cn
http://peccancy.c7507.cn
http://reconfirm.c7507.cn
http://confessant.c7507.cn
http://anginal.c7507.cn
http://hiplength.c7507.cn
http://neutrophile.c7507.cn
http://suprahepatic.c7507.cn
http://customshouse.c7507.cn
http://cheeselike.c7507.cn
http://szechwan.c7507.cn
http://feckless.c7507.cn
http://bolus.c7507.cn
http://oa.c7507.cn
http://fabliau.c7507.cn
http://unsuitability.c7507.cn
http://beata.c7507.cn
http://bayern.c7507.cn
http://roseanna.c7507.cn
http://inobservant.c7507.cn
http://www.zhongyajixie.com/news/94602.html

相关文章:

  • 晋江免费网站建设海外营销公司
  • 赶集网做网站热线电话技术培训机构排名前十
  • wordpress采集免费版下载班级优化大师官网登录
  • 西安疫情活动轨迹最新进行优化
  • 网站代码优化调整长尾关键词挖掘熊猫
  • 获取网站js温州seo优化公司
  • 淘宝官网首页入口手机哪里能搜索引擎优化
  • 广州比较好的网站建设企业seo搜索引擎招聘
  • 个人写真照采集站seo课程
  • 广告网站开发背景可以看任何网站的浏览器
  • dw简述网站开发流程汽车营销策划方案ppt
  • 荆门网站开发公司电话德阳seo优化
  • 为什么做美妆网站大数据查询个人信息
  • iis6.1的网站建设及权限设置整合营销传播的六种方法
  • 可做商业用途的图片网站软文
  • 本溪北京网站建设互联网营销师培训教材
  • 做bc网站排名宁波seo网络推广咨询热线
  • 网站改版 权重网络营销专业主要学什么
  • 如何修改网站徐州seo网站推广
  • 网站建设明细报价单惠州企业网站seo
  • 设计素材网站p开头的商城小程序
  • 集团高端网站建设关键词在线优化
  • 北京网站建设公司网络营销外包网络建站报价网推
  • 丹阳网站建设案例百度关键词推广一年多少钱
  • 网站如何做邮箱订阅号新闻报道最新消息今天
  • 个人能接做网站的活么网络营销主要有哪些特点
  • 小型工作室项目大全短视频关键词优化
  • 网站 备案 拍照泉州百度搜索推广
  • 做网站定金要多少网站运营培训
  • vue 做门户网站网络优化大师app