Target
Use mongoengine and models create as many page as U can, and all data resources is from mongoDB. Here is the web page we made in last homework.
Step by Step
With Explorer's inspector, I found where those title's should be.
Now I use contexts to substitute it:
Actually, context should be taken from database. So we have to connect by this code in settings.py:
from mongoengine import connect
connect('ganjiDB', host='127.0.0.1', port=27017)
Then made a class inherit from Document
Now edit views to enrich your context:
Now add a paginator
At last Made a page Navigator
All code changed in this Lesson
settings.py
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
from mongoengine import connect
connect('ganjiDB', host='127.0.0.1', port=27017)
views.py
from django.shortcuts import render
from blog_web.models import ItemInfo
from django.core.paginator import Paginator
def blog_view(request):
limit = 5
item_infos = ItemInfo.objects[:20]
pageinator = Paginator(item_infos, limit)
page = request.GET.get('page',1)
# for other pages
# http://localhost:8000/blog/?page=3
loaded = pageinator.page(page)
context = {
'ItemInfo': loaded
}
return render(request, 'index.html', context)
models.py
from django.db import models
from mongoengine import *
# Create your models here.
class ItemInfo(Document):
title = StringField()
cates = ListField(StringField())
area = ListField(StringField())
url = StringField()
look = StringField()
price = StringField()
pub_date = StringField()
time = StringField()
meta = {'collection': 'bjGanji'}
if __name__ == '__main__':
connect('ganjiDB', host='127.0.0.1', port=27017)
for i in ItemInfo.objects[:10]:
print(i.area)
index.html
<!-- A wrapper for all the blog posts -->
<div class="posts">
<h1 class="content-subhead">Pinned Post</h1>
{% for eachItem in ItemInfo %}
<!-- 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' %}">
<h2 class="post-title">{{ eachItem.title }}</h2>
<p class="post-meta">
Type {{ eachItem.cate }} at
{% for a in eachItem.area %}
<span class="meta-cate">{{ a }}</span>
{% endfor %}
</p>
</header>
<div class="post-description">
<p>
<a href="{{ url }}"> {{ eachItem.look }} </a> {{ eachItem.price }}
</p>
</div>
</section>
{% endfor %}
<div>
{% if ItemInfo.has_previous %}
<a href="?page={{ ItemInfo.previous_page_number }}">Previous Page</a>
{% endif %}
---{{ ItemInfo.number }} in {{ItemInfo.paginator.num_pages}}---
{% if ItemInfo.has_next %}
<a href="?page={{ ItemInfo.next_page_number }}">Next Page</a>
{% endif %}
</div>
</div>