Django----使用模板系统渲染博客页面

1、模板写法同Flask,可以参考之前的FLask-模板

2、将之前的BootStrap静态页面中的数据使用模板写

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Djanjo</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

     <!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

      <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
      <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container page-header">
    <h1>文章列表
    <small>by zhonghuihong </small></h1>

</div>
<div class="container page-body">
    <div class="col-md-9" role="main">
        <div class="body-main">
            {% for article in article_list%}------------->所以现在重点是要返回这个list,这个list是从view.py中的页面视图中返回
                            <div>
                <h2>{{ article.title }}</h2>
                <p>{{ article.brief_content }}</p>
            </div>
            {% endfor %}


        </div>
    </div>
    <div class="col-md-3" role="complementary">
        <div>
            <h2>最新文章</h2>
            {% for article in article_list %}
            <h4><a href="#">{{ article.title }}</a></h4>
            {% endfor %}

        </div>
    </div>
</div>

</body>
</html>

3、在view.py中新增一个视图函数,返回list

def get_index_page(requestss):
    all_article=Article.objects.all();
    return render(requestss,'blog/index.html',----------------->render()函数就是渲染页面,第一个函数写入参数,第二个参数指定这个函数要渲染的页面,这里要渲染的是'index.html'页面,第三个
参数是指定要返回到前端页面的数据,这里只需要返回一个list,前端根据for循环取出数据进行展示 {
'article_list':all_article });

4、然后配置应用路由(项目路由都是一样的,所以不用配置)

urlpatterns=[
    path('hello_world',hello_world),
    path('content',article_content),
path('Article_List',get_index_page)----------------->一定要注意这里没有get_index_page(),只写方法名,后面不加(),否则会报错
];

5、最终结果

猜你喜欢

转载自www.cnblogs.com/shenyexiaoqingxin/p/10625206.html
今日推荐