成果:

代碼:
在之后的步驟中在相信說明吧
步驟:
1.這個(gè)mongoengine需要pip安裝的pip install mongoengine命令即可實(shí)現(xiàn)
建立與數(shù)據(jù)庫的鏈接,connect的第一個(gè)參數(shù)是數(shù)據(jù)庫的名稱

2.在第一步之后,在models中
from django.db import models
from mongoengine import *
# from mongoengine import connect
# connect('ceshi', host='127.0.0.1', port=27017)
# Create your models here.
class ItemInfo(Document):
# StringField()的意思就是字符串
title = StringField()
# ListField的意思就是列表化一下
area = ListField(StringField())
pub_date = StringField()
look = StringField()
url = StringField()
cates = ListField(StringField())
time = StringField()
price = StringField()
meta = {
'collection': 'item_info',
}
# 親測(cè)可用
# for i in ItemInfo.objects[:1]:
# print(i.title)
我個(gè)人的理解是初始化數(shù)據(jù)庫,這個(gè)是數(shù)據(jù)庫事先存在的情況下,與數(shù)據(jù)庫建立連接。而且在class中,每一項(xiàng)右側(cè)都要跟數(shù)據(jù)庫的key一致(必須一樣,如果不一致就報(bào)錯(cuò)了)
可以看到,在前后注釋掉的東西可以用來驗(yàn)證是否成功
3.在view.py中
需要導(dǎo)入這個(gè)類
from ganji.models import ItemInfo
def index(request):
limit = 4
# 取出20個(gè)元素
item_info = ItemInfo.objects[:1]
# 下面的語句跟上面語句等價(jià),但是為什么索引在這里
# 仿佛失效的樣子
# item_info = ItemInfo.objects.all()
# 分頁器,前面是總共的元素,limit是每頁的元素個(gè)數(shù)
paginator = Paginator(item_info, limit)
print('頁碼數(shù)量', paginator.num_pages)
# 從頁面獲取頁數(shù)信息,這里是獲???后參數(shù)page的value,
# 可以看到get后的‘page’并不是我們定義的,而是我們獲取的
# 它的值就是個(gè)int
page = request.GET.get('page', 1)
# 顯示某‘page’頁面的信息
loaded = paginator.page(page)
# print('這里是loaded', loaded)
print('page_range', paginator.page_range)
context = {
'item_info': loaded,
}
# context是關(guān)鍵字,傳入字典,但是這個(gè)字典的key才是在HTML中用到的
# 這點(diǎn)跟flask不一樣,flask是隨便定義user=user,左邊是模板中的,右邊是views中的
return render(request, 'index.html', context)
4.同時(shí)在HTML文件中
寫入信息,用循環(huán)既可,這點(diǎn)跟jinja2很相似,但是需要注意的是:
我們用的是context的key,并不是context本身
{% for item in item_info %}
<section class="post">
<header class="post-header">
<h2 class="post-title">{{ item.title }}</h2>
<p class="post-meta">
{% for cate in item.cates %}
<a class="post-category post-category-design" href="#">{{ cate }}</a>
{% endfor %}
<br>
<br>
{% for a in item.area %}
<a class="post-category post-category-pure" href="#">{{ a }}</a>
{% endfor %}
</p>
</header>
<div class="post-description">
<p>
¥{{ item.price }}---{{ item.pub_date }}
</p>
</div>
</section>
{% endfor %}
5.分頁器
在views視圖函數(shù)中導(dǎo)入本次的重點(diǎn)--分頁器(詳細(xì)資料點(diǎn)擊查看)
from django.core.paginator import Paginator
需要在HTML中特意做出來這個(gè)分頁器。
其原理是:顯示頁碼,當(dāng)點(diǎn)擊上一頁下一頁的時(shí)候,會(huì)自動(dòng)給視圖函數(shù)傳參數(shù),然后根據(jù)這個(gè)參數(shù),返回?cái)?shù)據(jù)給HTML渲染
<div class="footer">
<div class="pure-menu pure-menu-horizontal">
<ul>
<li class="pure-menu-item"><a href="?page=1" class="pure-menu-link">首頁</a></li>
{% if item_info.has_previous %}
<li class="pure-menu-item"><a href="?page={{ item_info.previous_page_number }}" class="pure-menu-link">< pre</a></li>
{% endif %}
<li class="pure-menu-item"><span> {{ item_info.number }} of {{ item_info.paginator.num_pages }}</span></li>
{% if item_info.has_next %}
<li class="pure-menu-item"><a href="?page={{ item_info.next_page_number }}" class="pure-menu-link">next ></a></li>
{% endif %}
<li class="pure-menu-item"><a href="?page={{ item_info.paginator.num_pages }}" class="pure-menu-link">尾頁</a></li>
</ul>
</div>
</div>
其中用到了很多函數(shù),可以查看分頁器的詳細(xì)介紹
新技能GET:
1.分頁器--http://python.usyiyi.cn/django/topics/pagination.html
2.流程是:
setting中注冊(cè)數(shù)據(jù)庫->models中引入數(shù)據(jù)庫->views中使用數(shù)據(jù)庫->最終用數(shù)據(jù)庫渲染
遇到的坑:
1.在models中引入數(shù)據(jù)庫的時(shí)候
class ItemInfo(Document):
其中的對(duì)象是以大寫字母D開頭的Document
2.在views.py中,item_info = ItemInfo.objects[:1]是復(fù)數(shù)的objects,復(fù)數(shù)!
疑問未決:
如圖,第十行的代碼中索引好像完全不起作用,無論改為多少,都能在第15行初始化中加載所有的object!而在11行print(item_info)中還是1,到16行就已經(jīng)是所有數(shù)據(jù)都加載進(jìn)去了??!
