使用Django搭建网站实现商品分页功能


Posted in Python onMay 22, 2020

装好Django,写好index.html后,可以展示网页了。但是这只是静态页面,没有关联数据库,也不能分页展示商品信息。本节连接mongodb数据库(事先已准备好数据),从中取出几十条商品信息,每页展示4个商品信息,并具有翻页功能,做好的页面效果大致如下:

使用Django搭建网站实现商品分页功能

开始代码:

1、在settings.py(项目名称目录下)中,增加2段代码,分别是static文件夹位置和连接mongodb的代码:

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)  # 指定static文件夹位置

from mongoengine import connect
connect('ganji', host='127.0.0.1', port=27017)  # 连接ganji数据库

2、在models.py(本APP目录下)中,代码:

from django.db import models
from mongoengine import *
 
# Create your models here.
 
# 创建帖子信息类,继承自mongoengine的文件类<br data-filtered="filtered">class PostInfo(Document):  
  area = ListField(StringField())
  title = StringField()
  cates = ListField(StringField())
  price = StringField()
 
  pub_date = StringField()   # 数据集里面所有的字段都要有,就算不用也得列出来
  url = StringField()
  look = StringField()
  time = IntField()
  cates2 = StringField()
 
  meta = {'collection':'goods_info'}  # 定位好是goods_info数据集

3、在views.py(本APP目录下)中,代码:

from django.shortcuts import render
from sample_blog.models import PostInfo  # 导入已写好的数据结构
from django.core.paginator import Paginator # 导入分页器
 
# Create your views here.
def index(request):
  limit = 4 # 每页放几条帖子
  all_post_info = PostInfo.objects[:20] # 取前20个帖子的数据
  paginatior = Paginator(all_post_info, limit)  # 用分页器分页
  page_num = request.GET.get('page', 1) # 取request中的页码,取不到就为1
  loaded = paginatior.page(page_num) # 取page_num那一页的数据,一般是4条
 
  context = {
    # 首条固定的帖子信息
    'title': '三星 A5 白色',
    'des': '【图】三星 A5 白色 没有打开过 - 朝阳望京台式机/配件 - 北京58同城',
    'price': '1500',
    'area': ["朝阳", "望京"],
    'tag1': "北京二手市场",
    'tag2': "北京二手台式机/配件",
 
    # 每页更新的帖子信息
    'one_page_post': loaded
  }
  return render(request, 'index.html',context)

4、修改index.html文件,主要修改了有文字标注的部分:

<div class="posts">
       <h1 class="content-subhead">Pinned Post</h1>
 
       <!-- A single blog post -->
       <section class="post">
         <header class="post-header">
           <img class="post-avatar" alt="Tilo Mitra's avatar" height="48" width="48" src="{% static 'img/common/tilo-avatar.png' %}">





<!-- 修改了{{title}}等 -->
           <h2 class="post-title">{{ title }}</h2>
 
           <p class="post-meta">
             地区 <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="post-author">{{ area }}</a> under <a class="post-category post-category-design" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ tag1 }}</a> <a class="post-category post-category-pure" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{tag2}}</a>
           </p>
         </header>
 
         <div class="post-description">
           <p>
             {{ des }}|价格:{{ price }}
           </p>
         </div>
       </section>
     </div>
 
     <div class="posts">
       <h1 class="content-subhead">Recent Posts</h1><!-- 增加for循环,将one_page_post值带入 -->
       {% for item in one_page_post %}
         <section class="post">
           <header class="post-header">
             <img class="post-avatar" alt="Eric Ferraiuolo's avatar" height="48" width="48" src="{% static 'img/common/ericf-avatar.png' %}">
 
             <h2 class="post-title">{{ item.title }}</h2>
 
             <p class="post-meta">
               地区 <a class="post-author" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ item.area }}</a>分类<!-- 再增加一个for循环,把cates里的元素都展示出来 -->
               {% for cate in item.cates %}
                  <a class="post-category post-category-pure" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ cate }}</a>
               {% endfor %}
             </p>
           </header>
 
           <div class="post-description">
             <p>
               {{ item.title }}|价格:{{ item.price }}
             </p>
           </div>
         </section>
       {% endfor %}
 
     </div>



<!-- 增加本段div,实现页面底部可翻页 -->
      <div align="center">
       {% if one_page_post.has_previous %}
         <a href="?page={{ one_page_post.previous_page_number }}" rel="external nofollow" >< Pre</a>
       {% endif %}
         <span> {{ one_page_post.number }} of {{ one_page_post.paginator.num_pages }} </span>
       {% if one_page_post.has_next %}
         <a href="?page={{ one_page_post.next_page_number }}" rel="external nofollow" >Next ></a>
       {% endif %}
      </div>

5、附上urls.py(项目名称目录下)文件,本节中并没有修改,但也备注上:

from django.contrib import admin
from django.urls import path
from sample_blog.views import index
 
urlpatterns = [
  path('admin/', admin.site.urls),
  path('index/', index),
]

以上步骤完成后,启动服务(python manage.py runserver),访问http://127.0.0.1:8000/index/即可看到效果。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python写的PHPMyAdmin暴力破解工具代码
Aug 06 Python
介绍Python中的文档测试模块
Apr 28 Python
Python循环语句中else的用法总结
Sep 11 Python
Python闭包的两个注意事项(推荐)
Mar 20 Python
深入理解Python3中的http.client模块
Mar 29 Python
Python多线程应用于自动化测试操作示例
Dec 06 Python
Python实现简单层次聚类算法以及可视化
Mar 18 Python
python网络应用开发知识点浅析
May 28 Python
python3 下载网络图片代码实例
Aug 27 Python
Jupyter notebook快速入门教程(推荐)
May 18 Python
Python批量删除mysql中千万级大量数据的脚本分享
Dec 03 Python
python3 使用ssh隧道连接mysql的操作
Dec 05 Python
Tensorflow卷积实现原理+手写python代码实现卷积教程
May 22 #Python
Python实现发票自动校核微信机器人的方法
May 22 #Python
基于django micro搭建网站实现加水印功能
May 22 #Python
基于Tensorflow一维卷积用法详解
May 22 #Python
Python参数传递机制传值和传引用原理详解
May 22 #Python
python filecmp.dircmp实现递归比对两个目录的方法
May 22 #Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
May 22 #Python
You might like
php实现的在线人员函数库
2008/04/09 PHP
19个Android常用工具类汇总
2014/12/30 PHP
PHP aes (ecb)解密后乱码问题
2015/06/22 PHP
php strftime函数的详细用法
2018/06/21 PHP
TP3.2.3框架使用CKeditor编辑器在页面中上传图片的方法分析
2019/12/31 PHP
Google Suggest ;-) 基于js的动态下拉菜单
2006/10/11 Javascript
jQuery 淡出一个图像到另一个图像的实现代码
2013/06/12 Javascript
JS 数字转换为大写金额的简单实例
2016/08/04 Javascript
bootstrap table复杂操作代码
2016/11/01 Javascript
jQuery中table数据的值拷贝和拆分
2017/03/19 Javascript
关于Bootstrap按钮组件消除黄框的方法
2017/05/19 Javascript
最新Javascript程序员面试试题和解题方法
2017/11/23 Javascript
微信小程序实现文字无限轮播效果
2018/12/28 Javascript
JS实现选项卡效果的代码实例
2019/05/20 Javascript
微信小程序 WXML节点信息查询详解
2019/07/29 Javascript
微信小程序顶部导航栏可滑动并选中放大
2019/12/05 Javascript
json.stringify()与json.parse()的区别以及用处
2021/01/25 Javascript
Python中的if、else、elif语句用法简明讲解
2016/03/11 Python
Python中装饰器兼容加括号和不加括号的写法详解
2017/07/05 Python
Python网络爬虫中的同步与异步示例详解
2018/02/03 Python
Python实现简单的列表冒泡排序和反转列表操作示例
2019/07/10 Python
Python如何实现定时器功能
2020/05/28 Python
Python实现迪杰斯特拉算法过程解析
2020/09/18 Python
selenium携带cookies模拟登陆CSDN的实现
2021/01/19 Python
HTML5地理定位实例
2014/10/15 HTML / CSS
亚洲领先的旅游体验市场:Voyagin
2019/11/23 全球购物
人力资源管理毕业生自荐信
2013/11/21 职场文书
应届毕业生应聘自荐信
2013/12/07 职场文书
上班看电影检讨书
2014/02/12 职场文书
学习退步检讨书
2014/09/28 职场文书
入股协议书范本
2014/11/01 职场文书
预备党员自我评价范文
2015/03/04 职场文书
小学班级管理心得体会
2016/01/07 职场文书
小学生反邪教心得体会
2016/01/15 职场文书
详解JavaScript中Arguments对象用途
2021/08/30 Javascript
Python万能模板案例之matplotlib绘制甘特图
2022/04/13 Python