pycharm開發django工程(二) 項目需求: 1. 從mongodb中讀取數據,並顯示到網頁中 2. 在網頁顯示的每一頁加入分頁符 首先使用pycharm的企業版新建一個django的虛擬工程(參考我的上一個博客),這是初始的顯示效果 這是原始的html文件,css文件在本文的最後,至於圖片...
pycharm開發django工程(二)
項目需求:
1. 從mongodb中讀取數據,並顯示到網頁中
2. 在網頁顯示的每一頁加入分頁符
首先使用pycharm的企業版新建一個django的虛擬工程(參考我的上一個博客),這是初始的顯示效果
這是原始的html文件,css文件在本文的最後,至於圖片就隨意照一張改名就行
{% load static %} <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="A layout example that shows off a blog page with a list of posts."> <title>Blog – Layout Examples – Pure</title> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-old-ie-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-min.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog-old-ie.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog.css' %}"> </head> <body> <div class="content pure-u-1 pure-u-md-3-4"> <div> <!-- A wrapper for all the blog posts --> <div class="posts"> <h1 class="content-subhead">Pinned Post</h1> <!-- 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">Introducing Pure</h2> <p class="post-meta"> By <a href="#" class="post-author">Tilo Mitra</a> under <a class="post-category post-category-design" href="#">CSS</a> <a class="post-category post-category-pure" href="#">Pure</a> </p> </header> <div class="post-description"> <p> Yesterday at CSSConf, we launched Pure – a new CSS library. Phew! Here are the <a href="https://speakerdeck.com/tilomitra/pure-bliss">slides from the presentation</a>. Although it looks pretty minimalist, we’ve been working on Pure for several months. After many iterations, we have released Pure as a set of small, responsive, CSS modules that you can use in every web project. </p> </div> </section> </div> </div> </div> </body> </html>
首先要安裝mongoengine
pip3 install mongoengine然後在settings.py文件中定義mongodb的連接(要連接的mongodb資料庫名為test,使用到的是test資料庫里的一張名為sample的表)
from mongoengine import connect
connect('test', host='127.0.0.1', port=27017)
在models.py文件中定義讀取的資料庫欄位
from django.db import models from mongoengine import * connect('test', host='127.0.0.1', port=27017) # 指明要連接的資料庫 class ArtiInfo(Document): title = StringField() url = StringField() price = StringField() pub_date = StringField() look = StringField() area = ListField(StringField()) # 定義列表類型 cates = ListField(StringField()) meta = { 'collection': 'sample'} # 指明連接資料庫的哪張表 for i in ArtiInfo.objects[:10]: # 測試是否連接成功 print(i.title)
在這裡可以直接運行models.py文件,看是否能讀出資料庫里的內容,註意要把資料庫中的欄位全部定義出來
修改views.py文件,定義要傳遞到html文件中的內容
from django.shortcuts import render from myblog.models import ArtiInfo def index(request): article = ArtiInfo.objects[:10] #只顯示前10個內容 context = { 'ArtiInfo':article } return render(request, 'index.html', context) # 傳遞context參數
修改index.html文件,在文件中迴圈顯示從資料庫中讀取的數據
{% load static %} <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="A layout example that shows off a blog page with a list of posts."> <title>Blog – Layout Examples – Pure</title> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-old-ie-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-min.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog-old-ie.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog.css' %}"> </head> <body> <div class="content pure-u-1 pure-u-md-3-4"> <div> <!-- A wrapper for all the blog posts --> <div class="posts"> <h1 class="content-subhead">Pinned Post</h1> {% for item in ArtiInfo %} <!-- 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' %}"> <h3 class="post-title">{{ item.title }}</h3> <p class="post-meta"> By <a href="#" class="post-author">{{ item.pub_date }}</a> {% for each in item.cates %} <a class="post-category post-category-design" href="#">{{ each }}</a> {% endfor %} </p> </header> <div class="post-description"> <p> {{ item.url }} </p> </div> </section> {% endfor %} </div> </div> </div> </body> </html>
這是從資料庫里讀取的效果
添加分頁器
在上面的views.py文件中添加分頁器
from django.shortcuts import render from myblog.models import ArtiInfo from django.core.paginator import Paginator def index(request): limit = 1 #限制每一頁顯示的條目數量 article = ArtiInfo.objects paginator = Paginator(article, limit) page_num = request.GET.get('page', 1) #從url中獲取頁碼參數 loaded = paginator.page(page_num) context = { 'ArtiInfo':loaded } return render(request, 'index.html', context)
在Index.html文件的末尾處添加分頁器
{% load static %} <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="A layout example that shows off a blog page with a list of posts."> <title>Blog – Layout Examples – Pure</title> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-old-ie-min.css' %}"> <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/grids-responsive-min.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog-old-ie.css' %}"> <link rel="stylesheet" href="{% static 'css/layouts/blog.css' %}"> </head> <body> <div class="content pure-u-1 pure-u-md-3-4"> <div> <!-- A wrapper for all the blog posts --> <div class="posts"> <h1 class="content-subhead">Pinned Post</h1> {% for item in ArtiInfo %} <!-- 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' %}"> <h3 class="post-title">{{ item.title }}</h3> <p class="post-meta"> By <a href="#" class="post-author">{{ item.pub_date }}</a> {% for each in item.cates %} <a class="post-category post-category-design" href="#">{{ each }}</a> {% endfor %} </p> </header> <div class="post-description"> <p> {{ item.url }} </p> </div> </section> {% endfor %} </div> <div class="main-content-pagitor"> {% if ArtiInfo.has_previous %} <a href="?page={{ ArtiInfo.previous_page_number }}"> 上一頁</a> {% endif %} <span>{{ ArtiInfo.number }} of {{ ArtiInfo.paginator.num_pages }}</span> {% if ArtiInfo.has_next %} <a href="?page={{ ArtiInfo.next_page_number }}">下一頁</a> {% endif %} </div> </div> </div> </body> </html>
這是最終的顯示效果
本文中使用到的css文件如下:
* { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } a { text-decoration: none; color: rgb(61, 146, 201); } a:hover, a:focus { text-decoration: underline; } h3 { font-weight: 100; } /* LAYOUT CSS */ .pure-img-responsive { max-width: 100%; height: auto; } #layout { padding: 0; } .header { text-align: center; top: auto; margin: 3em auto; } .sidebar { background: rgb(61, 79, 93); color: #fff; } .brand-title, .brand-tagline { margin: 0; } .brand-title { text-transform: uppercase; } .brand-tagline { font-weight: 300; color: rgb(176, 202, 219); } .nav-list { margin: 0; padding: 0; list-style: none; } .nav-item { display: inline-block; *display: inline; zoom: 1; } .nav-item a { background: transparent; border: 2px solid rgb(176, 202, 219); color: #fff; margin-top: 1em; letter-spacing: 0.05em; text-transform: uppercase; font-size: 85%; } .nav-item a:hover, .nav-item a:focus { border: 2px solid rgb(61, 146, 201); text-decoration: none; } .content-subhead { text-transform: uppercase; color: #aaa; border-bottom: 1px solid #eee; padding: 0.4em 0; font-size: 80%; font-weight: 500; letter-spacing: 0.1em; } .content { padding: 2em 1em 0; } .post { padding-bottom: 2em; } .post-title { font-size: 2em; color: #222; margin-bottom: 0.2em; } .post-avatar { border-radius: 50px; float: right; margin-left: 1em; } .post-description { font-family: Georgia, "Cambria", serif; color: #444; line-height: 1.8em; } .post-meta { color: #999; font-size: 90%; margin: 0; } .post-category { margin: 0 0.1em; padding: 0.3em 1em; color: #fff; background: #999; font-size: 80%; } .post-category-design { background: #5aba59; } .post-category-pure { background: #4d85d1; } .post-category-yui { background: #8156a7; } .post-category-js { background: #df2d4f; } .post-images { margin: 1em 0; } .post-image-meta { margin-top: -3.5em; margin-left: 1em; color: #fff; text-shadow: 0 1px 1px #333; } .footer { text-align: center; padding: 1em 0; } .footer a { color: #ccc; font-size: 80%; } .footer .pure-menu a:hover, .footer .pure-menu a:focus { background: none; } @media (min-width: 48em) { .content { padding: 2em 3em 0; margin-left: 25%; } .header { margin: 80% 2em 0; text-align: right; } .sidebar { position: fixed; top: 0; bottom: 0; } } .main-content-pagitor { width: 50%; padding: 10px 20px 5px 20px; overflow: auto; margin: auto; /*position: relative;*/ text-align: center; } .main-content-pagitor a { color: #cccccc; padding: 0 5px 0 5px; } .main-content-pagitor span { color: #585858; /*padding: 20px 20px 20px 20px;*/ }