色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

使用Django搭建網(wǎng)站實(shí)現(xiàn)商品分頁功能

瀏覽:44日期:2024-09-30 15:54:28

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

使用Django搭建網(wǎng)站實(shí)現(xiàn)商品分頁功能

開始代碼:

1、在settings.py(項(xiàng)目名稱目錄下)中,增加2段代碼,分別是static文件夾位置和連接mongodb的代碼:

STATIC_URL = ’/static/’STATICFILES_DIRS = (os.path.join(BASE_DIR,’static’),) # 指定static文件夾位置from mongoengine import connectconnect(’ganji’, host=’127.0.0.1’, port=27017) # 連接ganji數(shù)據(jù)庫

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

from django.db import modelsfrom 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ù)集里面所有的字段都要有,就算不用也得列出來 url = StringField() look = StringField() time = IntField() cates2 = StringField() meta = {’collection’:’goods_info’} # 定位好是goods_info數(shù)據(jù)集

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

from django.shortcuts import renderfrom sample_blog.models import PostInfo # 導(dǎo)入已寫好的數(shù)據(jù)結(jié)構(gòu)from django.core.paginator import Paginator # 導(dǎo)入分頁器 # Create your views here.def index(request): limit = 4 # 每頁放幾條帖子 all_post_info = PostInfo.objects[:20] # 取前20個(gè)帖子的數(shù)據(jù) paginatior = Paginator(all_post_info, limit) # 用分頁器分頁 page_num = request.GET.get(’page’, 1) # 取request中的頁碼,取不到就為1 loaded = paginatior.page(page_num) # 取page_num那一頁的數(shù)據(jù),一般是4條 context = { # 首條固定的帖子信息 ’title’: ’三星 A5 白色’, ’des’: ’【圖】三星 A5 白色 沒有打開過 - 朝陽望京臺(tái)式機(jī)/配件 - 北京58同城’, ’price’: ’1500’, ’area’: ['朝陽', '望京'], ’tag1’: '北京二手市場(chǎng)', ’tag2’: '北京二手臺(tái)式機(jī)/配件', # 每頁更新的帖子信息 ’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 alt='Tilo Mitra’s avatar' src='http://www.lshqa.cn/bcjs/{% static ’img/common/tilo-avatar.png’ %}'><!-- 修改了{(lán){title}}等 --> <h2 class='post-title'>{{ title }}</h2> <p class='post-meta'> 地區(qū) <a href='http://www.lshqa.cn/bcjs/17280.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' class='post-author'>{{ area }}</a> under <a href='http://www.lshqa.cn/bcjs/17280.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >{{ tag1 }}</a> <a href='http://www.lshqa.cn/bcjs/17280.html#' 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 alt='Eric Ferraiuolo’s avatar' src='http://www.lshqa.cn/bcjs/{% static ’img/common/ericf-avatar.png’ %}'> <h2 class='post-title'>{{ item.title }}</h2> <p class='post-meta'> 地區(qū) <a href='http://www.lshqa.cn/bcjs/17280.html#' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' rel='external nofollow' >{{ item.area }}</a>分類<!-- 再增加一個(gè)for循環(huán),把cates里的元素都展示出來 --> {% for cate in item.cates %} <a href='http://www.lshqa.cn/bcjs/17280.html#' 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)頁面底部可翻頁 --> <div align='center'> {% if one_page_post.has_previous %} <a href='http://www.lshqa.cn/bcjs/?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='http://www.lshqa.cn/bcjs/?page={{ one_page_post.next_page_number }}' rel='external nofollow' >Next ></a> {% endif %} </div>

5、附上urls.py(項(xiàng)目名稱目錄下)文件,本節(jié)中并沒有修改,但也備注上:

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

以上步驟完成后,啟動(dòng)服務(wù)(python manage.py runserver),訪問http://127.0.0.1:8000/index/即可看到效果。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 国产三级做爰高清在线 | 国产成人福利夜色影视 | 美国毛片一级视频在线aa | 日韩亚洲欧美一区二区三区 | 伊人99re| 久久精品网站免费观看 | 国产精品久久久久久久久福利 | 丝袜精品 欧美 亚洲 自拍 | 日产国产精品久久久久久 | 伊人手机视频 | 欧美毛片aaa激情 | 老师张开腿让我捅 | 日韩精品一二三区 | 91人人视频国产香蕉 | 91精品国产乱码久久久久久 | 日韩二区三区 | 亚洲资源在线播放 | 一区二区国产在线播放 | 亚洲欧美在线不卡 | 美女被男人桶到嗷嗷叫爽网站 | 99视频精品全部 在线 | avav男人天堂 | 99视频网 | 亚洲六月丁香六月婷婷蜜芽 | a毛片免费视频 | 99久久精品免费国产一区二区三区 | 中文字幕在线日韩 | 三级黄色在线 | 欧美精品亚洲精品日韩专区 | 嫩小性性性xxxxbbbb | 91国内视频在线观看 | 99久久99久久久99精品齐 | 一级看片免费视频 | 日韩一级欧美一级毛片在 | 亚洲欧美日韩国产专区一区 | 韩国一级片视频 | 国产2区 | 91久久精品一区二区 | 亚洲 欧美 日韩 丝袜 另类 | 国产亚洲精品成人一区看片 | 这里只有精品国产 |