美文网首页
Week4 hw2: Use Models to create

Week4 hw2: Use Models to create

作者: 快要没时间了 | 来源:发表于2016-06-12 11:49 被阅读0次

    Target

    The Blog we made in last class

    Use mongoengine and models create as many page as U can, and all data resources is from mongoDB. Here is the web page we made in last homework.

    DB date structure

    Step by Step

    With Explorer's inspector, I found where those title's should be.


    Before Change Anything

    Now I use contexts to substitute it:

    after replacement
    made context After replacement

    Actually, context should be taken from database. So we have to connect by this code in settings.py:

    from mongoengine import connect
    connect('ganjiDB', host='127.0.0.1', port=27017)
    

    Then made a class inherit from Document

    made a new class according to DB Structure

    Now edit views to enrich your context:

    Use context

    Now add a paginator

    From Right to Left

    At last Made a page Navigator

    Page Navi

    All code changed in this Lesson

    settings.py

    # Static files (CSS, JavaScript, Images)
    # https://docs.djangoproject.com/en/1.9/howto/static-files/
    
    STATIC_URL = '/static/'
    STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
    
    from mongoengine import connect
    connect('ganjiDB', host='127.0.0.1', port=27017)
    

    views.py

    from django.shortcuts import render
    from blog_web.models import ItemInfo
    from django.core.paginator import Paginator
    
    def blog_view(request):
        limit = 5
        item_infos = ItemInfo.objects[:20]
        pageinator = Paginator(item_infos, limit)
        page = request.GET.get('page',1)
        # for other pages
        # http://localhost:8000/blog/?page=3
        loaded = pageinator.page(page)
    
        context = {
            'ItemInfo': loaded
        }
    
        return render(request, 'index.html', context)
    

    models.py

    from django.db import models
    from mongoengine import *
    
    
    # Create your models here.
    class ItemInfo(Document):
        title = StringField()
        cates = ListField(StringField())
        area = ListField(StringField())
        url = StringField()
        look = StringField()
        price = StringField()
        pub_date = StringField()
        time = StringField()
        meta = {'collection': 'bjGanji'}
    
    
    if __name__ == '__main__':
        connect('ganjiDB', host='127.0.0.1', port=27017)
        for i in ItemInfo.objects[:10]:
            print(i.area)
    

    index.html

                <!-- A wrapper for all the blog posts -->
                <div class="posts">
                    <h1 class="content-subhead">Pinned Post</h1>
    
                    {% for eachItem in ItemInfo %}
                    <!-- 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">{{ eachItem.title }}</h2>
    
                                <p class="post-meta">
                                    Type {{ eachItem.cate }} at
                                    {% for a in eachItem.area  %}
                                        <span class="meta-cate">{{ a }}</span>
                                    {% endfor %}
                                </p>
                            </header>
    
                            <div class="post-description">
                                <p>
                                    <a href="{{ url }}"> {{ eachItem.look }} </a> {{ eachItem.price }}
                                </p>
                            </div>
                        </section>
                    {% endfor %}
    
                    <div>
                        {% if ItemInfo.has_previous %}
                            <a href="?page={{ ItemInfo.previous_page_number }}">Previous Page</a>
                        {% endif %}
                        ---{{ ItemInfo.number }} in {{ItemInfo.paginator.num_pages}}---
                        {% if ItemInfo.has_next %}
                            <a href="?page={{ ItemInfo.next_page_number }}">Next Page</a>
                        {% endif %}
                    </div>
                </div>
    

    相关文章

      网友评论

          本文标题:Week4 hw2: Use Models to create

          本文链接:https://www.haomeiwen.com/subject/nbasdttx.html