美文网首页
(八)丰富首页内容

(八)丰富首页内容

作者: 顽强的猫尾草 | 来源:发表于2018-05-23 09:38 被阅读9次
    1. 静态资源文件放在static文件夹下:
    static/
    +- css/
    |  +- addons/
    |  |  +- uikit.addons.min.css
    |  |  +- uikit.almost-flat.addons.min.css
    |  |  +- uikit.gradient.addons.min.css
    |  +- awesome.css
    |  +- uikit.almost-flat.addons.min.css
    |  +- uikit.gradient.addons.min.css
    |  +- uikit.min.css
    +- fonts/
    |  +- fontawesome-webfont.eot
    |  +- fontawesome-webfont.ttf
    |  +- fontawesome-webfont.woff
    |  +- FontAwesome.otf
    +- js/
       +- awesome.js
       +- html5.js
       +- jquery.min.js
       +- uikit.min.js
    
    1. 通过uikit框架完成父模板__base__.html的编写(templates文件夹下):
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        {% block meta %}<!-- 用于子页面定义一些meta, 例如RSS feed -->{% endblock %}
        <title>{% block title %}<!-- 页面的标题 --> ? {% endblock %} - Awesome Python Webapp</title>
        <link rel="stylesheet" href="/static/css/uikit.min.css">
        <link rel="stylesheet" href="/static/css/uikit.grdient.min.css">
        <link rel="stylesheet" href="/static/css/awesome.css">
        <script src="/static/js/jquery.min.js"></script>
        <script src="/static/js/md5.js"></script>
        <script src="/static/js/uikit.min.js"></script>
        <script src="/static/js/awesome.js"></script>
        {% block beforehead %}<!-- 子页面可以在<head>标签关闭前插入JavaScript代码 -->{% endblock %}
    </head>
    <body>
        <!-- 导航条 -->
        <nav class="uk-navbar uk-navbar-attached uk-margin-bottom">
            <div class="uk-container uk-container-center">
                <!-- 标题和栏目 -->
                <a href="/" class="uk-navbar-brand">Awesome</a>
                <ul class="uk-navbar-nav">
                    <li data-url="blogs"><a href="/"><i class="uk-icon-home"></i> 日志</a></li>
                    <!-- 在新窗口中打开链接 -->
                    <li><a target="_blank" href="#"><i class="uk-icon-book"></i> 教程</a></li>
                    <li><a target="_blank" href="#"><i class="uk-icon-code"></i> 源码</a></li>
                </ul>
                <!-- 用户信息 -->
                <div class="uk-navbar-flip">
                    <ul class="uk-navbar-nav">
                    {% if user %}
                        <!-- 下拉菜单 -->
                        <li class="uk-parent" data-uk-dropdown>
                            <a href="#0"><i class="uk-icon-user"></i> {{ user.name }}</a>
                            <div class="uk-dropdown uk-dropdown-navbar">
                                <ul class="uk-nav uk-nav-navbar">
                                    <li><a href="/signout"><i class="uk-icon-sign-out"></i> 登出</a></li>
                                </ul>
                            </div>
                        </li>
                    {% else %}
                        <li><a href="/signin"><i class="uk-icon-sign-in"></i> 登陆</a></li>
                        <li><a href="/register"><i class="uk-icon-edit"></i> 注册</a></li>
                    {% endif %}
                    </ul>
                </div>
            </div>
        </nav>
    
        <div class="uk-container uk-container-center">
            <div class="uk-grid">
                {% block content %}<!-- 子页面的content布局和内容 -->{% endblock %}
            </div>
        </div>
    
        <!-- 颜色简写只适用于css -->
        <div class="uk-margin-large-top" style="background-color:#eee; border-top:1px solid #ccc;">
            <div class="uk-container uk-container-center uk-text-center">
                <div class="uk-panel uk-margin-top uk-margin-bottom">
                    <p>
                        <a terget="_blank" href="#" class="uk-icon-button uk-icon-weibo"></a>
                        <a terget="_blank" href="#" class="uk-icon-button uk-icon-github"></a>
                        <a terget="_blank" href="#" class="uk-icon-button uk-icon-linkedin-square"></a>
                        <a terget="_blank" href="#" class="uk-icon-button uk-icon-twitter"></a>
                    </p>
                    <p>Powered by <a href="#">Awesome Python Webapp</a>. Copyright &copy; 2017.  [<a href="/manage/" target="_blank">Manage</a>]</p>
                    <p><a href="http:www.lx.com/" target="_blank">www.lx.com</a>. All rights reserved.</p>
                    <a target="_blank" href="#"><i class="uk-icon-html5" style="font-size:64px; color: #444;"></i></a>
                </div>
            </div>
        </div>
    </body>
    </html>
    
    1. 从__base__.html继承一个blogs.html,覆写里面的block:
    {% extends '__base__.html' %}
    
    {% block title %}日志{% endblock %}
    
    {% block content %}
    
        <div class="uk-width-medium-3-4">
        {% for blog in blogs %}
            <article class="uk-article">
                <h2><a href="/blog/{{ blog.id }}">{{ blog.name }}</a></h2>
                <!-- 通过jinja2的filter(过滤器), 把一个浮点数转换成日期字符串 -->
                <p class="uk-article-meta">发表于{{ blog.created_at|datetime }}</p>
                <p>{{ blog.summary }}</p>
                <p><a href="/blog/{{ blog.id }}">继续阅读 <i class="uk-icon-angle-double-right"></i></a></p>
            </article>
            <hr class="uk-article-divider">
        {% endfor %}
        </div>
    
        <div class="uk-width-medium-1-4">
            <div class="uk-panel uk-panel-header">
                <h3 class="uk-panel-title">友情链接</h3>
                <ul class="uk-list uk-list-line">
                    <li><i class="uk-icon-thumbs-o-up"></i> <a target="_blank" href="#">编程</a></li>
                    <li><i class="uk-icon-thumbs-o-up"></i> <a target="_blank" href="#">读书</a></li>
                    <li><i class="uk-icon-thumbs-o-up"></i> <a target="_blank" href="#">Python教程</a></li>
                    <li><i class="uk-icon-thumbs-o-up"></i> <a target="_blank" href="#">Git教程</a></li>
                </ul>
            </div>
        </div>
    {% endblock %}
    
    1. 为了测试效果,相应地调整一下handlers.py中的首页处理函数:
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    import re, time, json, logging, hashlib, base64, asyncio
    from coroweb import get, post
    from models import User, Comment, Blog, next_id
    
    # 参数aiohttp.web.request实例,包含了所有浏览器发送过来的HTTP协议里面的信息
    @get('/')
    async def index(request):
        summary = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
        blogs = [
            Blog(id='1', name='Test Blog', summary=summary, created_at=time.time()-120),
            Blog(id='2', name='Write Sth', summary=summary, created_at=time.time()-3600),
            Blog(id='3', name='Learn Swift', summary=summary, created_at=time.time()-7200)
        ]
        return {'__template__': 'blogs.html', 'blogs': blogs}
    
    1. 测试下效果:


    不过里面的东西都还点不了,只是有了大体的样貌。

    相关文章

      网友评论

          本文标题:(八)丰富首页内容

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