Django模板语言要点速记

2016-08-24  本文已影响0人  糖果遇见猫

View层

context = {
    'ArtiInfo':loaded
    'title':'just a title'
    #相当于'ArtInfo' = loaded
    #相当于'title'='just a title'
}
<h3><a href="#">{{ item.title }}</a></h3>

model层

from mongoengine import connect
connect('wbsite', host='127.0.0.1', port=27017)
class ArtiInfo(Document):
    des = StringField()
    title = StringField()
    scores = StringField()
    tags = ListField(StringField())
    #指定需要读取的sheet
    meta = {'collection':'arti_info3'}
for i in ArtiInfo.objects[:1]:
    print(i.title,i.des,i.scores,i.tags)

分页器(Paginator)

from django.core.paginator import Paginator
Paginator.png
def index(request):
    limit = 4
    arti_info = ArtiInfo.objects[:20]
    paginatior = Paginator(arti_info,limit)
    page = request.GET.get('page',1)
    print(request)
    print(request.GET)
    loaded = paginatior.page(page)
    context = {
        'ArtiInfo':loaded
    }
    return render(request,'index.html',context)

Django模板语言

{% for item in ArtiInfo %}
    <li>
        <img src="{% static 'images/0001.jpg' %}" width="100" height="91">
        <div class="article-info">
            <h3><a href="#">{{ item.title }}</a></h3>
            <p class="meta-info">
                {% for tag in item.tags %}
                    <span class="meta-cate">{{ tag }}</span>
                {% endfor %}
            </p>
            <p class="description">{{ item.des }}</p>
        </div>
        <div class="rate">
            <span class="rate-score">{{ item.scores }}</span>
        </div>
    </li>
{% endfor %}
上一篇 下一篇

猜你喜欢

热点阅读