博客

Python+Django搭建个人博客(4)-Django模板

2020-03-28  本文已影响0人  DayBreakL

模板演示

理解模版系统

在views.py中指定渲染某个模版,如:

def index(request):
    return render(request,'blog_index.html')

Django会自动搜寻各个App的templates文件夹,然后在blog/templates/blog_index.html中找到。

Django模板进阶

    ![image](https://img.haomeiwen.com/i12041448/5ae6bde97962f70b?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    ![image](https://img.haomeiwen.com/i12041448/065ce87e2cceec15?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    找到所有css、js、图像,修改链接如下:
    
    原:
    
    `<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">`
    
    修改为:
    
    `<link href="{%static 'blog/vendor/bootstrap/css/bootstrap.min.css'%}" rel="stylesheet">`
    
    搜索关键字 css、js、img,注意还要在原路径下加`blog/`:
    
    `<header class="masthead" style="background-image: url('{%static 'blog/img/home-bg.jpg'%}')">`
    
    `<script src="{%static 'blog/vendor/jquery/jquery.min.js'%}"></script>`

    `<script src="{%static 'blog/vendor/bootstrap/js/bootstrap.bundle.min.js'%}"></script>`
    
    
    ![image](https://img.haomeiwen.com/i12041448/564d118f89b30feb?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    如果报错可以在终端上或者网页控制台看看怎么回事,例如:
    
    ![image](https://img.haomeiwen.com/i12041448/ed4017160ff15922?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    路径有问题,404,就是忘了加`blog/`
    ![image](https://img.haomeiwen.com/i12041448/e83e4a70c147f3f4?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    
    ![image](https://img.haomeiwen.com/i12041448/c29de0cb5c094543?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
- 修改代码如下
    ```html
    {% for post in post_list %}
    <div class="post-preview">
      <a href="{{post.link}}">
        <h2 class="post-title">
          {{post.title}}
        </h2>
        <h3 class="post-subtitle">
          {{post.subtitle}}
        </h3>
      </a>
      <p class="post-meta">Posted by
        <a href="#">{{post.author}}</a>
        {{post.date}}</p>
    </div>
    <hr>
    {% endfor %}
    ```

作业

image
上一篇 下一篇

猜你喜欢

热点阅读