PyCharm 2020.2.4 (Professional Edition)
Django version 3.1
MongoDB 3.4.10
Python 3.7.4
bootstrap 3.3.7
pymongo 3.11.3
User
├─.idea
│ ├─dataSources
│ └─inspectionProfiles
├─templates
├─User
│ └─__pycache__
└─__pycache__
User/urls.py
"""User URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
import views
urlpatterns = [
path('admin/', admin.site.urls),
path('user', views.user)
]
views.py
[hostname]
为主机名,本机为localhost
[port]
为端口号,默认为27017
[database]
为数据库名[collection]
为集合名from django.shortcuts import render
import pymongo
def user(request):
mongo_client = pymongo.MongoClient("mongodb://[hostname]:[port]/")
db = mongo_client["database"]
user_collection = db["collection"]
users = []
for user in user_collection.find():
users.append({'id': user["_id"], 'name': user['name'], 'phone': user['phone']})
return render(request, 'user.html', {'users': users})
templates/user.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div style="width: 500px; margin: 0 auto;">
<div class="page-header">
<h1><span class="label label-primary">User List</span></h1>
</div>
<table class="table table-bordered table-hover ">
<thead>
<tr>
<td><span class="label label-info">ID</span></td>
<td><span class="label label-info">User Name</span></td>
<td><span class="label label-info">Phone</span></td>
</tr>
</thead>
<tbody>
{% for user in users %}
<tr class="info">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.phone }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
db.createCollection('user', autoIndexId=true)
db.user.insertMany([
{
name: 'Tony',
phone: '123456',
password: '123456'
},
{
name: 'Tom',
phone: '123456',
password: '123456'
},
{
name: 'Tina',
phone: '123456',
password: '123456'
}
])
http://127.0.0.1:8000/user
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Usertitle>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
head>
<body>
<div style="width: 500px; margin: 0 auto;">
<div class="page-header">
<h1><span class="label label-primary">User Listspan>h1>
div>
<table class="table table-bordered table-hover ">
<thead>
<tr>
<td><span class="label label-info">IDspan>td>
<td><span class="label label-info">User Namespan>td>
<td><span class="label label-info">Phonespan>td>
tr>
thead>
<tbody>
<tr class="info">
<td>60371a89383a6b16b0e00a76td>
<td>Tonytd>
<td>123456td>
tr>
<tr class="info">
<td>60371ade383a6b16b0e00a77td>
<td>Tomtd>
<td>123456td>
tr>
<tr class="info">
<td>60371ade383a6b16b0e00a78td>
<td>Tinatd>
<td>123456td>
tr>
tbody>
table>
div>
body>
html>
Boostrap3
PyMongo