使用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线程创建和终止实例代码
Jan 20 Python
基于Python在MacOS上安装robotframework-ride
Dec 28 Python
Laravel+Dingo/Api 自定义响应的实现
Feb 17 Python
了解不常见但是实用的Python技巧
May 23 Python
win8.1安装Python 2.7版环境图文详解
Jul 01 Python
基于Python安装pyecharts所遇的问题及解决方法
Aug 12 Python
Kears+Opencv实现简单人脸识别
Aug 28 Python
解决import tensorflow as tf 出错的原因
Apr 16 Python
Keras 切换后端方式(Theano和TensorFlow)
Jun 19 Python
PyTorch中model.zero_grad()和optimizer.zero_grad()用法
Jun 24 Python
matplotlib基础绘图命令之imshow的使用
Aug 13 Python
python scipy 稀疏矩阵的使用说明
May 26 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 array_search() 函数使用
2010/04/13 PHP
PHP和.net中des加解密的实现方法
2013/02/27 PHP
采用header定义为文件然后readfile下载(隐藏下载地址)
2014/01/31 PHP
php读取文件内容的方法汇总
2015/01/24 PHP
PHP去除字符串最后一个字符的三种方法实例
2017/03/01 PHP
js+css使DIV始终居于屏幕中间 左下 左上 右上 右下的代码集合
2011/03/10 Javascript
JavaScript面向对象(极简主义法minimalist approach)
2012/07/17 Javascript
jquery入门必备的基本认识及实例(整理)
2013/06/24 Javascript
js中parseInt函数浅谈
2013/07/31 Javascript
js或jquery实现页面打印可局部打印
2014/03/27 Javascript
Javascript的严格模式strict mode详细介绍
2014/06/06 Javascript
js仿微信语音播放实现思路
2016/12/12 Javascript
JS封装通过className获取元素的函数示例
2016/12/20 Javascript
利用imgareaselect辅助后台实现图片上传裁剪
2017/03/02 Javascript
基于JavaScript实现前端数据多条件筛选功能
2020/08/19 Javascript
vue 项目常用加载器及配置详解
2018/01/22 Javascript
通过函数作用域和块级作用域看javascript的作用域链
2018/08/05 Javascript
ant-design-vue中的select选择器,对输入值的进行筛选操作
2020/10/24 Javascript
浅谈Python中copy()方法的使用
2015/05/21 Python
python3操作mysql数据库的方法
2017/06/23 Python
深入理解Django的自定义过滤器
2017/10/17 Python
python使用pil进行图像处理(等比例压缩、裁剪)实例代码
2017/12/11 Python
python实现的登录与提交表单数据功能示例
2019/09/25 Python
用python中的matplotlib绘制方程图像代码
2019/11/21 Python
ITK 实现多张图像转成单个nii.gz或mha文件案例
2020/07/01 Python
详解基于python的图像Gabor变换及特征提取
2020/10/26 Python
详解Python利用configparser对配置文件进行读写操作
2020/11/03 Python
详解Html5中video标签那些属性和方法
2019/07/01 HTML / CSS
德国宠物用品、宠物食品及水族馆网上商店:ZooRoyal
2017/07/09 全球购物
美国学校用品、教室和教学商店:Discount School Supply
2018/04/04 全球购物
捷克移动配件网上商店:ProMobily.cz
2019/03/15 全球购物
领导干部对照检查材料
2014/08/24 职场文书
商场父亲节活动方案
2014/08/27 职场文书
一份文言文检讨书
2014/09/13 职场文书
导师鉴定意见
2015/06/05 职场文书
MySQL transaction事务安全示例讲解
2022/06/21 MySQL