1.連接到MongoDB并插入數(shù)據(jù)
# 連接的數(shù)據(jù)庫(kù)名和集合名 可以隨便寫(xiě)
# 插入完成后 MongoDB自動(dòng)創(chuàng)建
from pymongo import MongoClient
myclient = MongoClient("mongodb://localhost:27017/")
db=myclient['WB']
wb=db['wb']
wb.insert_many(hot_list)
2. 在Django獲得MongoDB數(shù)據(jù)
在settings.py中連接到數(shù)據(jù)庫(kù)
import pymongo
client = pymongo.MongoClient()
db = client['WB']
在views.py中查找數(shù)據(jù),并返回給頁(yè)面
from yaoqi.settings import db
def comic_list(request):
result = db.wb.find()
content = {'result': result}
return render(request, 'base.html', content)
在base.html 頁(yè)面中展示出來(lái)

image.png