理解上下文
使用模板语言
制作分页器
#Note
##理解上下文
###views.py
1 2 render(request,x.html,context) 传入三个函数 { { id } } , context = { id1='' ,id2='' , }
###models.py
from mongoengine import *
##模板语言
1 2 3 { % for i in Artinfo%} { % endfor %}
##分页器
from django.core.paginator import Paginator
#Aim
用58的数据库和昨天的模板生成一个博客
#Result
Code html格式化 项目下载
models.py
链接数据库
写个类,里面所有属性和数据库条目名字同名
meta = 'collection' : 'sheet_out'
链接对于collection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 from django.db import modelsfrom mongoengine import *from mongoengine import connectconnect('ilw' ,host='localhost' ,port = 27017 ) class ArtiInfo (Document) : price = StringField() place = StringField() title = StringField() comment = StringField() url = StringField() meta = {'collection' :'sheet_out' }
views.py
设置每页上限
生成一个翻页对象
加载当前页
1 2 3 4 5 6 7 8 9 10 11 12 13 from django.shortcuts import renderfrom django.core.paginator import Paginatorfrom django_web.models import ArtiInfodef index (request) : limit = 3 paginator = Paginator(ArtiInfo.objects,limit) page = request.GET.get('page' ,1 ) loaded = paginator.page(page) content = {'ArtiInfo' : loaded} return render(request,'index.html' ,content)
index.html__生成条目
for && endear
{ {代替id} }代替
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 {% for it in ArtiInfo %} <div class ="posts" > <h1 class ="content-subhead" > Recent Posts</h1 > <section class ="post" > <header class ="post-header" > <img class ="post-avatar" alt ="Eric Ferraiuolo's avatar" height ="48" width ="48" src ={% static "img /common /ericf-avatar.png " %}> <h2 class ="post-title" > {{ it.title }}</h2 > <p class ="post-meta" > By <a class ="post-author" href ="#" > {{ it.place }}</a > under <a class ="post-category post-category-js" href ="#" > {{ it.comment }}</a > </p > </header > <div class ="post-description" > <p > <a href ="{{ it.url }}" > {{ it.price }} </a > </p > </div > </section > {% endfor %}
如果用到list
1 2 3 {% for tag in item.tags %} <span class="meta-cate">{{ tag }}</span> {% endfor %}
index.html__生成页码 1 2 3 4 5 6 7 8 9 </div class = "main-content-pagitor" > {% if ArtiInfo.has_previous %} <a href ="?page={{ ArtiInfo.previous_page_number }}" > Pre</a > {% endif %} <span > {{ ArtiInfo.number }} of {{ ArtiInfo.paginator.num_pages }}</span > {% if ArtiInfo.has_next %} <a href ="?page={{ ArtiInfo.next_page_number }}" > Next</a > {% endif %} </div >
总结 这一章模板内容相对较多…不过对于自己博客理解加深了一层还是很开心啦~~
<
hexo debug && GitLearn
python实战计划 用 Django 搭建简单网页
>