需求分析
将 MongoDB 数据库内的数据读取出来,并展示在前端页面。
在 settings 中建立连接
连结信息写在 settings.py 中,同时注释原有的 sqlite 数据库连接。
setting.py1 2 3 4 5 6 7 8 9 10 11
| mongo_con = mongoengine.connect('***', host='your ip address', port=27017, username="***", password="***") collection = str(datetime.date.today()) mongo_db = mongo_con[collection]
DATABASES = { 'default': { } }
|
在 models 中声明需要查询的集合以及字段
声明需要查询的字段,与你的数据库表结构对应。
models.py1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import datetime
from mongoengine import *
connect('163news', host='117.78.0.242', port=27017, username="zouyi", password="980718")
class NetEase(Document): _id = ObjectIdField(required=True) url = StringField(required=True) title = StringField(required=True) date = StringField(required=True) content = StringField(required=True) abstract = StringField(required=True) id = IntField(required=True) comments = StringField(required=True) heat = IntField(required=True) collection = str(datetime.date.today()) meta = {'collection': collection}
|
在 views 内获取相应的信息
同时在 views.py 中获取所需信息,并使用 paginator 分页,通过 render 传给html。
分页可查看 Django使用分页器
views.py1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.shortcuts import render
from django.views.decorators.clickjacking import xframe_options_exempt
from NetEase.models import NetEase
@xframe_options_exempt def news(request): limit = 4 article = NetEase.objects paginator = Paginator(article, limit) page_num = request.GET.get('page', 1) page_num = int(page_num) try: loaded = paginator.page(page_num) except ValueError: loaded = paginator.page(1) except PageNotAnInteger: loaded = paginator.page(1) except EmptyPage: loaded = paginator.page(paginator.num_pages) context = { 'NetEase': loaded } return render(request, 'NetEase/news.html', context)
|
对应信息展示在前端
遍历查询到的数据,通过 item.xxx
加载到 html。
news.html1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| {% for item in NetEase %} <div class="news1"> <h3> <div class="left"> {# <span class="red">全国</span>#} <span>{{ item.title }}</span> <span><img src="{% static 'img/new.gif' %}" alt="new"/></span> <span class="ls">[正/负面信息]</span> </div> <span class="right">0</span> </h3> <div class="h3bottom1"> <span class="feb900">{{ item.date }}</span> {% if item.content|length >= 300 %} {{ item.content|slice:"300" }} …… {% else %}{{ item.content }} {% endif %} <span style="font-weight: bold;text-decoration: underline;"> <a href="{{ item.url }}" target="_blank">阅读原文</a> </span> </div> <div class="butt"> <a href="#"><img src="{% static 'img/xxlist_03_15.png' %}" alt=""/></a> <a href="#"><img src="{% static 'img/xxlist_03_13.png' %}" alt=""/></a> <a href="#"><img src="{% static 'img/xxlist_03_11.png' %}" alt=""/></a> <a href="#"><img src="{% static 'img/xxlist_03_09.png' %}" alt=""/></a> <a href="#"><img src="{% static 'img/xxlist_03_07.png' %}" alt=""/></a> </div> <div class="clear"></div> </div> {% endfor %}
|
完整效果: