使用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中%r和%s的详解及区别
Mar 16 Python
python实现杨辉三角思路
Jul 14 Python
Python使用正则表达式过滤或替换HTML标签的方法详解
Sep 25 Python
基于Python __dict__与dir()的区别详解
Oct 30 Python
基于并发服务器几种实现方法(总结)
Dec 29 Python
Django rest framework实现分页的示例
May 24 Python
Python2和Python3.6环境解决共存问题
Nov 09 Python
python 实现查询Neo4j多节点的多层关系
Dec 23 Python
python列表返回重复数据的下标
Feb 10 Python
基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解
Jun 08 Python
PyQt QMainWindow的使用示例
Mar 24 Python
python开发人人对战的五子棋小游戏
May 02 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
一首老MP3,致敬WAR3经典
2021/03/08 魔兽争霸
全文搜索和替换
2006/10/09 PHP
从C/C++迁移到PHP——判断字符类型的函数
2006/10/09 PHP
PHP 强制性文件下载功能的函数代码(任意文件格式)
2010/05/26 PHP
php数组对百万数据进行排除重复数据的实现代码
2010/06/08 PHP
php文件上传表单摘自drupal的代码
2011/02/15 PHP
php自动获取字符串编码函数mb_detect_encoding
2011/05/31 PHP
php DOS攻击实现代码(附如何防范)
2012/05/29 PHP
php Xdebug的安装与使用详解
2013/06/20 PHP
ThinkPHP之import方法实例详解
2014/06/20 PHP
跟我学Laravel之请求(Request)的生命周期
2014/10/15 PHP
PHP使用ODBC连接数据库的方法
2015/07/18 PHP
深入理解JS addLoadEvent函数
2016/05/20 Javascript
原生JS封装ajax 传json,str,excel文件上传提交表单(推荐)
2016/06/21 Javascript
IE8兼容Jquery.validate.js的问题
2016/12/01 Javascript
JS实现获取图片大小和预览的方法完整实例【兼容IE和其它浏览器】
2017/04/24 Javascript
ES6中新增的Object.assign()方法详解
2017/09/22 Javascript
[01:04:22]2018DOTA2亚洲邀请赛 3.31 小组赛 B组 IG vs EG
2018/04/01 DOTA
Python和perl实现批量对目录下电子书文件重命名的代码分享
2014/11/21 Python
Django框架中方法的访问和查找
2015/07/15 Python
python批量导入数据进Elasticsearch的实例
2018/05/30 Python
Python如何发布程序的详细教程
2018/10/09 Python
pygame实现雷电游戏雏形开发
2018/11/20 Python
Pycharm运行加载文本出现错误的解决方法
2019/06/27 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2019/08/04 Python
日本索尼音乐商店:Sony Music Shop
2018/07/17 全球购物
意大利奢侈品购物网站:Deliberti
2019/10/08 全球购物
在校硕士自我鉴定
2014/01/23 职场文书
小学学校评估方案
2014/06/08 职场文书
2014年销售工作总结范文
2014/12/01 职场文书
毕业生党员个人总结
2015/02/14 职场文书
民事诉讼代理词
2015/05/25 职场文书
golang elasticsearch Client的使用详解
2021/05/05 Golang
python实现简单的井字棋
2021/05/26 Python
CSS变量实现主题切换的方法
2021/06/23 HTML / CSS
MySQL的安装与配置详细教程
2021/06/26 MySQL