欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Django搭建網站實現商品分頁功能

 更新時間:2020年05月22日 10:36:22   作者:djl_djl  
這篇文章主要介紹了使用Django搭建網站實現商品分頁功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

裝好Django,寫好index.html后,可以展示網頁了。但是這只是靜態(tài)頁面,沒有關聯數據庫,也不能分頁展示商品信息。本節(jié)連接mongodb數據庫(事先已準備好數據),從中取出幾十條商品信息,每頁展示4個商品信息,并具有翻頁功能,做好的頁面效果大致如下:

開始代碼:

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.
 
# 創(chuàng)建帖子信息類,繼承自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">
             地區(qū) <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循環(huán),將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">
               地區(qū) <a class="post-author" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >{{ item.area }}</a>分類<!-- 再增加一個for循環(huán),把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(項目名稱目錄下)文件,本節(jié)中并沒有修改,但也備注上:

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內置函數之filter map reduce介紹

    Python內置函數之filter map reduce介紹

    Python內置了一些非常有趣、有用的函數,如:filter、map、reduce,都是對一個集合進行處理,filter很容易理解用于過濾,map用于映射,reduce用于歸并. 是Python列表方法的三架馬車
    2014-11-11
  • Python之ThreadPoolExecutor線程池問題

    Python之ThreadPoolExecutor線程池問題

    這篇文章主要介紹了Python之ThreadPoolExecutor線程池問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Python基礎學習之認識線程

    Python基礎學習之認識線程

    這篇文章主要介紹了Python線程,這篇開始我們將進入中級編程。處理更加復雜事情。比如本文的線程,咱們先從基礎知識入手,需要的朋友可以參考下下面文章的詳細內容
    2022-02-02
  • Python 多張圖片合并成一個pdf的參考示例

    Python 多張圖片合并成一個pdf的參考示例

    最近需要將記的筆記整理成一個pdf進行保存,所以就研究了一下如何利用 Python 代碼將拍下來的照片整個合并成一個pdf
    2021-06-06
  • Python3批量創(chuàng)建Crowd用戶并分配組

    Python3批量創(chuàng)建Crowd用戶并分配組

    這篇文章主要介紹了Python3批量創(chuàng)建Crowd用戶并分配組,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • Python中的模塊和包概念介紹

    Python中的模塊和包概念介紹

    這篇文章主要介紹了Python中的模塊和包概念介紹,本文講解了模塊概述、模塊名稱空間、導入模塊、導入模塊屬性(from … import …)、包等內容,需要的朋友可以參考下
    2015-04-04
  • python繪制發(fā)散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖

    python繪制發(fā)散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖

    這篇文章主要介紹了python繪制發(fā)散型柱狀圖+誤差陰影時間序列圖+雙坐標系時間序列圖+繪制金字塔圖,詳細的內容需要的小伙伴可以參考一下下面文章內容
    2022-08-08
  • python多線程同步售票系統(tǒng)

    python多線程同步售票系統(tǒng)

    這篇文章主要介紹了python多線程同步售票系統(tǒng),文章基于python的相關資料展開詳細的多線程同步售票系統(tǒng)介紹,感興趣的小伙伴可以參考一下
    2022-05-05
  • Python使用asyncio異步時的常見問題總結

    Python使用asyncio異步時的常見問題總結

    這篇文章主要為大家整理了開發(fā)人員在?Python?中使用?asyncio?時提出的常見問題以及解決方法,文中的示例代碼講解詳細,感興趣的可以學習一下
    2023-04-04
  • Python基于Pymssql模塊實現連接SQL Server數據庫的方法詳解

    Python基于Pymssql模塊實現連接SQL Server數據庫的方法詳解

    這篇文章主要介紹了Python基于Pymssql模塊實現連接SQL Server數據庫的方法,較為詳細的分析了pymssql模塊的下載、安裝及連接、操作SQL Server數據庫的相關實現技巧,需要的朋友可以參考下
    2017-07-07

最新評論