多适用于:整版以图片为主,大小不一的图片按照一定的规律排列的网页布局。

1:创建model类,并生成数据表

from django.db import models# Create your models here.
# 图片表
class Img(models.Model):url=models.FileField(max_length=32,verbose_name="图片路径",upload_to='static/upload')title=models.CharField(max_length=16,verbose_name='标题')summary=models.CharField(max_length=128,verbose_name='简介')class Meta:verbose_name_plural='图片'def __str__(self):return self.title

model

2:注册到django后台管理页面中,并创建管理员

from django.contrib import admin
from app01 import models
# Register your models here.
admin.site.register(models.Img)

admin.py

3:添加对应的文件夹,修改settings配置,准备数据

"""
Django settings for WaterfallFlow project.Generated by 'django-admin startproject' using Django 2.1.5.For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""import os# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'p5)q997@c#&(xtv79l24+(u-%3z$=ozv4_khe4$sz)$z$f=8cx'# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = TrueALLOWED_HOSTS = []# Application definition

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','app01.apps.App01Config',
]MIDDLEWARE = ['django.middleware.security.SecurityMiddleware','django.contrib.sessions.middleware.SessionMiddleware','django.middleware.common.CommonMiddleware','django.middleware.csrf.CsrfViewMiddleware','django.contrib.auth.middleware.AuthenticationMiddleware','django.contrib.messages.middleware.MessageMiddleware','django.middleware.clickjacking.XFrameOptionsMiddleware',
]ROOT_URLCONF = 'WaterfallFlow.urls'TEMPLATES = [{'BACKEND': 'django.template.backends.django.DjangoTemplates','DIRS': [os.path.join(BASE_DIR, 'templates')],'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',],},},
]WSGI_APPLICATION = 'WaterfallFlow.wsgi.application'# Database
# https://docs.djangoproject.com/en/2.1/ref/settings/#databases

DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR, 'db.sqlite3'),}
}# Password validation
# https://docs.djangoproject.com/en/2.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',},{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',},{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',},{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',},
]# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/

LANGUAGE_CODE = 'en-us'TIME_ZONE = 'UTC'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = True# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'),
)

Setting.py

4:url配置

"""WaterfallFlow URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf1. Import the include() function: from django.urls import include, path2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.conf.urls import url
from django.urls import path
from app01.ImgTest import ImgViewurlpatterns = [# path('admin/', admin.site.urls),url(r'^admin/', admin.site.urls),url(r'^img1.html$',ImgView.img1),url(r'^getImgs$', ImgView.getImgs),]

url配置

5:View设置

from django.shortcuts import render
from django.http import JsonResponse
from app01.models import Img# Create your views here.
def img1(request):return render(request,"ImgTest/img1.html")def getImgs(request):nid = request.GET.get('nid')img_list = Img.objects.filter(id__gt=nid).values('id','url','title')img_list=list(img_list)print(img_list)ret = {'status':True,'data':img_list}return JsonResponse(ret)

Views

6:html 页面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><script src="/static/js/jquery-3.3.1.min.js"></script><style>.w {width: 1000px;margin: 0px;}.item {width: 25%;float: left;}.item img {width: 100%;}</style>
</head>
<body>
<div class="w" id="container"><div class="item">11</div><div class="item">22</div><div class="item">33</div><div class="item">44</div></div>
<script>$(function(){initImg();})var nd=0;function initImg() {$.ajax({url:'getImgs',type:"GET",data:{nid:nd},datatype:'json',success:function(arg){var img_list=arg.data;$.each(img_list,function(index,v){var eqv=index%4;var tag = document.createElement("img");tag.src='/'+v.url;$("#container").children().eq(eqv).append(tag)})}})}
</script>
</body>
</html>

img1.html

以上这种方法有个弊端是:一次获取所有的数据库数据。完善:只完善部分数据,当滚轮滚到页面最下方的时候再次请求数据获取数据。

from django.shortcuts import render
from django.http import JsonResponse
from app01.models import Img
from django.db.models import Q# Create your views here.
def img1(request):return render(request,"ImgTest/img1.html")def getImgs(request):nid = request.GET.get('nid')nid2=int(nid)+10img_list = Img.objects.filter(Q(id__gt=nid)&Q(id__lt=nid2)).values('id','url')img_list=list(img_list)print(img_list)ret = {'status':True,'data':img_list}return JsonResponse(ret)

view

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><script src="/static/js/jquery-3.3.1.min.js"></script><style>.w {width: 1000px;margin: 0px;}.item {width: 25%;float: left;}.item img {width: 100%;}</style>
</head>
<body>
<div class="w" id="container"><div class="item">11</div><div class="item">22</div><div class="item">33</div><div class="item">44</div></div>
<script>$(function () {initImg();scrollEvent();})var nd = 0;function initImg() {$.ajax({url: 'getImgs',type: "GET",data: {nid: nd},datatype: 'json',success: function (arg) {var img_list = arg.data;$.each(img_list, function (index, v) {var eqv = index % 4;var tag = document.createElement("img");tag.src = '/' + v.url;$("#container").children().eq(eqv).append(tag)//当循环到最后一个图片时,将图片的id赋值给ndif(index+1==img_list.length){nd=v.id;}})}})}{#当滚轮滚动到底部时,执行initImg()#}
function scrollEvent() {$(window).scroll(function () {//什么时候表示滚动到底部{#文档高度= 窗口高度+滚动条高度#}var docHeight=$(document).height();//文档高度var winHeight=$(window).height();//窗口高度var scrHeight = $(window).scrollTop();//滚动条高度if (winHeight+scrHeight==docHeight){console.log(1)initImg();}})}</script>
</body>
</html>

html

以上这种方法还有瑕疵,就是:定义了公共属性nd,我们可以把公共属性nd封装到对象中

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><script src="/static/js/jquery-3.3.1.min.js"></script><style>.w {width: 1000px;margin: 0px;}.item {width: 25%;float: left;}.item img {width: 100%;}</style>
</head>
<body>
<div class="w" id="container"><div class="item">11</div><div class="item">22</div><div class="item">33</div><div class="item">44</div></div>
<script>$(function () {var obj = new ScrollImg();obj.initImg();obj.scrollEvent();})function ScrollImg() {{#为了去掉全局变量,创建一个新类#}this.id = 0;this.initImg = function () {var that = this;$.ajax({url: 'getImgs',type: "GET",data: {nid: that.id},datatype: 'json',success: function (arg) {var img_list = arg.data;$.each(img_list, function (index, v) {var eqv = index % 4;var tag = document.createElement("img");tag.src = '/' + v.url;$("#container").children().eq(eqv).append(tag)//当循环到最后一个图片时,将图片的id赋值给ndif (index + 1 == img_list.length) {that.id = v.id;}})}})}this.scrollEvent = function () {{#当滚轮滚动到底部时,执行initImg()#}var that = this;$(window).scroll(function () {//什么时候表示滚动到底部{#文档高度= 窗口高度+滚动条高度#}var docHeight = $(document).height();//文档高度var winHeight = $(window).height();//窗口高度var scrHeight = $(window).scrollTop();//滚动条高度if (winHeight + scrHeight == docHeight) {console.log(1)that.initImg();}})}}</script>
</body>
</html>

html

转载于:https://www.cnblogs.com/YK2012/p/10353232.html

饮冰三年-人工智能-Python-29瀑布流相关推荐

  1. apollo修改配置刷新bean_饮冰三年-人工智能-Python-57-Apollo之04应用 - 逍遥小天狼

    4.1 Apollo工作原理 下图是Apollo架构模块的概览 各模块职责 上图简要描述了Apollo的总体设计,我们可以从下往上看: Config Service提供配置的读取.推送等功能,服务对象 ...

  2. python在人工智能应用锁_饮冰三年-人工智能-Python-35权限管理(万能的权限通用模块)...

    自定义权限认证 1:修改model.py类.主要是添加两个class from django.db importmodelsfrom django.contrib.auth.models import ...

  3. c#和python_饮冰三年-人工智能-Python-10之C#与Python的对比

    开篇的话:任何⼀⻔语⾔都不是完美的,建议各位不要拿⼀个语⾔的劣势去跟另⼀个语⾔的优势来去⽐较,语⾔只是⼀个⼯具.正如天龙八部里面,萧峰萧大侠用太祖长拳PK少林功夫一般.存在即合理!我想应该不用我说了吧 ...

  4. python的人工智能模块_饮冰三年-人工智能-Python-17Python基础之模块与包

    一.模块(module) 1.1 啥是模块 简单理解一个.py文件就称之为一个模块. 有的功能开发者自己无法完成,需要借助已经实现的函数\类来完成这些功能.例如:和操作系统打交道的类或者是随机数函数等 ...

  5. 饮冰三年-人工智能-Python-16Python基础之迭代器、生成器、装饰器

    一:迭代器: 最大的特点:节省内存 1.1 迭代器协议 a:对象必须提供一个next方法, b:执行方法要么返回迭代中的下一项,要么抛弃一个Stopiteration异常, c:只能向后不能向前. 1 ...

  6. 饮冰三年-人工智能-Python-33博客园山寨版之报障管理

    报障单的内容和其实和文章管理内容类似 1:创建数据模型 class Trouble(models.Model):'''报障单'''uuid = models.UUIDField(primary_key ...

  7. 饮冰三年-人工智能-Python-17Python基础之模块与包

    一.模块(modue) 简单理解一个.py文件就称之为一个模块. 1.1 模块种类: python标准库第三方模板应用程序自定义模块(尽量不要与内置函数重名) View Code 1.2 模块导入方法 ...

  8. 饮冰三年-人工智能-linux-07 硬盘分区、格式化及文件系统的管理

    先给虚拟机添加一个硬盘 通过fdisk -l sdb,查看磁盘内容 通过fdisk /sdb 来操作分区 创建一个新分区 创建第二个分区 创建第三个分区 创建扩展分区 再次创建分区,其实使用的是扩展分 ...

  9. Python爬取瀑布流百度图片

    Python爬去瀑布流百度图片 import requests from bs4 import BeautifulSoup import re from urllib.parse import url ...

最新文章

  1. 2016030204 - git和github结合
  2. 前端面试官,我为什么讨厌你。
  3. 英特尔王锐:软硬件并驾齐驱,开发者是真英雄
  4. Openstack rabbitMQ 安装与配置
  5. cocos2d-x游戏开发(八)各类构造器
  6. Java四种线程池的使用
  7. HTML基础之bit哥的反客为主之道(9)
  8. 信息学奥赛一本通(1101:不定方程求解)
  9. access订单明细表怎么做_图书销售订单明细表 (4)
  10. java平台无关性_为什么Java能够实现平台无关性?
  11. 官网下载到离线的Adobe Acrobat Reader DC
  12. 2021-06-21属性选择器
  13. Elasticsearch搜索引擎(一)——基础使用
  14. 微信支付的架构到底有多牛?
  15. 二分查找法及二分搜索树及其C++实现
  16. HTTP500内部服务器错误
  17. 宝塔linux面板命令大全
  18. 漂亮的红色玫瑰花——情人节-圣诞节专属-代码实现
  19. ipod video 乔布斯的三个故事
  20. css打造超能陆战队--大白

热门文章

  1. 看我如何跨虚拟机实现Row Hammer攻击和权限提升
  2. Mybatis中的collection、association来处理结果映射
  3. thinkphp3.1的新功能
  4. 测试你的开源 IQ 答案
  5. all resources based on handshake
  6. not receive messages for the subscriptions setting
  7. 关于U打包代码必须放到Editor目录下的问题
  8. C#开发VS LUA开发
  9. Spring Cloud + Spring Boot + Mybatis + shiro + RestFul + 微服务
  10. 利用btrace在线监控java程序状态