使用Django搭建網(wǎng)站實(shí)現(xiàn)商品分頁(yè)功能
裝好Django,寫(xiě)好index.html后,可以展示網(wǎng)頁(yè)了。但是這只是靜態(tài)頁(yè)面,沒(méi)有關(guān)聯(lián)數(shù)據(jù)庫(kù),也不能分頁(yè)展示商品信息。本節(jié)連接mongodb數(shù)據(jù)庫(kù)(事先已準(zhǔn)備好數(shù)據(jù)),從中取出幾十條商品信息,每頁(yè)展示4個(gè)商品信息,并具有翻頁(yè)功能,做好的頁(yè)面效果大致如下:
開(kāi)始代碼:
1、在settings.py(項(xiàng)目名稱目錄下)中,增加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數(shù)據(jù)庫(kù)
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() # 數(shù)據(jù)集里面所有的字段都要有,就算不用也得列出來(lái) url = StringField() look = StringField() time = IntField() cates2 = StringField() meta = {'collection':'goods_info'} # 定位好是goods_info數(shù)據(jù)集
3、在views.py(本APP目錄下)中,代碼:
from django.shortcuts import render from sample_blog.models import PostInfo # 導(dǎo)入已寫(xiě)好的數(shù)據(jù)結(jié)構(gòu) from django.core.paginator import Paginator # 導(dǎo)入分頁(yè)器 # Create your views here. def index(request): limit = 4 # 每頁(yè)放幾條帖子 all_post_info = PostInfo.objects[:20] # 取前20個(gè)帖子的數(shù)據(jù) paginatior = Paginator(all_post_info, limit) # 用分頁(yè)器分頁(yè) page_num = request.GET.get('page', 1) # 取request中的頁(yè)碼,取不到就為1 loaded = paginatior.page(page_num) # 取page_num那一頁(yè)的數(shù)據(jù),一般是4條 context = { # 首條固定的帖子信息 'title': '三星 A5 白色', 'des': '【圖】三星 A5 白色 沒(méi)有打開(kāi)過(guò) - 朝陽(yáng)望京臺(tái)式機(jī)/配件 - 北京58同城', 'price': '1500', 'area': ["朝陽(yáng)", "望京"], 'tag1': "北京二手市場(chǎng)", 'tag2': "北京二手臺(tái)式機(jī)/配件", # 每頁(yè)更新的帖子信息 'one_page_post': loaded } return render(request, 'index.html',context)
4、修改index.html文件,主要修改了有文字標(biāo)注的部分:
<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' %}"> <!-- 修改了{(lán){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 }}|價(jià)格:{{ 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>分類<!-- 再增加一個(gè)for循環(huán),把cates里的元素都展示出來(lái) --> {% 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 }}|價(jià)格:{{ item.price }} </p> </div> </section> {% endfor %} </div> <!-- 增加本段div,實(shí)現(xiàn)頁(yè)面底部可翻頁(yè) --> <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(項(xiàng)目名稱目錄下)文件,本節(jié)中并沒(méi)有修改,但也備注上:
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), ]
以上步驟完成后,啟動(dòng)服務(wù)(python manage.py runserver),訪問(wèn)http://127.0.0.1:8000/index/即可看到效果。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于django micro搭建網(wǎng)站實(shí)現(xiàn)加水印功能
- python3.6+django2.0+mysql搭建網(wǎng)站過(guò)程詳解
- Django框架搭建的簡(jiǎn)易圖書(shū)信息網(wǎng)站案例
- Python+Django搭建自己的blog網(wǎng)站
- Python3創(chuàng)建Django項(xiàng)目的幾種方法(3種)
- Django 實(shí)現(xiàn) Websocket 廣播、點(diǎn)對(duì)點(diǎn)發(fā)送消息的代碼
- 部署Django到阿里云服務(wù)器教程示例
- Python Django搭建網(wǎng)站流程圖解
相關(guān)文章
Python內(nèi)置函數(shù)之filter map reduce介紹
Python內(nèi)置了一些非常有趣、有用的函數(shù),如:filter、map、reduce,都是對(duì)一個(gè)集合進(jìn)行處理,filter很容易理解用于過(guò)濾,map用于映射,reduce用于歸并. 是Python列表方法的三架馬車2014-11-11Python之ThreadPoolExecutor線程池問(wèn)題
這篇文章主要介紹了Python之ThreadPoolExecutor線程池問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Python基礎(chǔ)學(xué)習(xí)之認(rèn)識(shí)線程
這篇文章主要介紹了Python線程,這篇開(kāi)始我們將進(jìn)入中級(jí)編程。處理更加復(fù)雜事情。比如本文的線程,咱們先從基礎(chǔ)知識(shí)入手,需要的朋友可以參考下下面文章的詳細(xì)內(nèi)容2022-02-02Python3批量創(chuàng)建Crowd用戶并分配組
這篇文章主要介紹了Python3批量創(chuàng)建Crowd用戶并分配組,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05python繪制發(fā)散型柱狀圖+誤差陰影時(shí)間序列圖+雙坐標(biāo)系時(shí)間序列圖+繪制金字塔圖
這篇文章主要介紹了python繪制發(fā)散型柱狀圖+誤差陰影時(shí)間序列圖+雙坐標(biāo)系時(shí)間序列圖+繪制金字塔圖,詳細(xì)的內(nèi)容需要的小伙伴可以參考一下下面文章內(nèi)容2022-08-08Python使用asyncio異步時(shí)的常見(jiàn)問(wèn)題總結(jié)
這篇文章主要為大家整理了開(kāi)發(fā)人員在?Python?中使用?asyncio?時(shí)提出的常見(jiàn)問(wèn)題以及解決方法,文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2023-04-04Python基于Pymssql模塊實(shí)現(xiàn)連接SQL Server數(shù)據(jù)庫(kù)的方法詳解
這篇文章主要介紹了Python基于Pymssql模塊實(shí)現(xiàn)連接SQL Server數(shù)據(jù)庫(kù)的方法,較為詳細(xì)的分析了pymssql模塊的下載、安裝及連接、操作SQL Server數(shù)據(jù)庫(kù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07