create account

[Python #11] [Django #4] 听说 Bootstrap 可以很容易给网页穿样式? by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 · (edited)
$9.29
[Python #11] [Django #4] 听说 Bootstrap 可以很容易给网页穿样式?
pixabay
https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png

本期介绍如何用 Bootstrap 给 [上期](https://steemit.com/hive-101145/@june0620/python-10-django-3-steem)制作的网页穿上衣服。像我这样的 html, css 小菜鸟, Bootstrap最好不过了。 

##### 创建 html 文件及设置
👇 同上期,在 templates 文件夹创建 album.html和 base.html,之后修改 views.py 里的 template_name 改为 album.html。 
```
$ touch templates/base.html
$ touch templates/album.html
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/rGH2vnR3-image.png)

👇 如其名,base.html 是一个基文件。在当前情况其实也不需要这文件,直接写在album.html 文件下也可,但为了以后的维护还是准守规则吧。
在文件内写入 django 的命令和 bootstrap 的css路径。 
```
# templates/base.html

{% load static %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">

{% spaceless %}
{% endspaceless %}

{% block content %}
{% endblock content %}
```
👇 album.html 需要继承 base.html,在此文件内写入详细的 html 代码。
首先让我们看下如何使用 django 命令。有个继承 base.html 的代码,意味着继承 Bootstrap 的 css。
`{% block content %}`与 `{% endblock content %}`之间写入详细的 html 代码即可,这些代码也可以用 Bootstrap 的模板。不需要的部分便从模板删除后用 `{% for post in all_posts_list %}`, `{% endfor %}`命令给网页循环映射 STEEM 列表。 
<sub> 📝 Bootstrap 模板可以在 [官方例句](https://getbootstrap.com/docs/4.0/examples/)的 album 获取代码。</sub>
```
# templates/album.html

{% extends 'base.html' %}

{% block content %}

{% for post in all_posts_list %}
{% endfor %}

{% endblock content %}
```

👇 整体代码如下,比较简单。运行服务器后,访问可以看到穿好衣服的 STEEM 目录,还支持手机版。 

![image.png](https://files.peakd.com/file/peakd-hive/june0620/ll4AUVaG-image.png)



```
{% extends 'base.html' %}
{% load post_extras %}

{% block content %}

<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Steem Blog</title>
  </head>
  <body>
      <header>
          <div class="navbar navbar-dark bg-dark shadow-sm">
              <div class="container d-flex justify-content-between">
                  <a href="#" class="navbar-brand d-flex align-items-center">
                      <svg xmlns="https://www.w3.org/2000/svg" width="20" height="20" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" aria-hidden="true" class="mr-2" viewBox="0 0 24 24" focusable="false">
                          <path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/>
                          <circle cx="12" cy="13" r="4"/>
                      </svg>
                      <strong>Steem Blog</strong>
                  </a>
              </div>
          </div>
      </header>
      <main role="main">
          <div class="album py-5 bg-light">
              <div class="container">
                  <div class="row">
                      {% for post in all_posts %}
                      <div class="col-md-4 d-flex pb-3">
                          <div class="card mb-4 shadow-sm">
                              {% with post.comment.json_metadata|str_to_dict as extras %}
                              <a href="https://www.steemit.com/@{{post.comment.author}}/{{ post.comment.permlink }}" target="_blank">
                                  <img src="{{ extras.image.0 }}" class="card-img-top" alt="" height="220" width="512">
                              </a>
                              {% endwith %}
                              <div class="card-body">
                                  <p class="card-text">{{ post.comment.root_title }}</p>
                                  <div class="d-flex justify-content-between align-items-center">
                                      <div class="btn-group">
                                      </div>
                                      <small class="text-muted">댓글 {{ post.comment.children }} </small>
                                      <small class="text-muted">보팅 {{ post.comment.active_votes|length }} </small>
                                      <small class="text-muted">보상 {{ post.comment.pending_payout_value }} </small>
                                  </div>
                              </div>
                          </div>
                      </div>
                      {% endfor %}
                  </div>
              </div>
          </div>
      </main>
    </body>
</html>
{% endblock content %}
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/EPOWKLgx-image.png)

![image.png](https://files.peakd.com/file/peakd-hive/june0620/59zaWPQD-image.png)



.
.
.
.
[Cookie 😅]
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3

👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 2 others
properties (23)
authorjune0620
permlinkpython-11-django-4-bootstrap-qeup1c
categoryhive-105017
json_metadata{"app":"peakd/2020.08.2","format":"markdown","image":["https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png","https://files.peakd.com/file/peakd-hive/june0620/rGH2vnR3-image.png","https://files.peakd.com/file/peakd-hive/june0620/ll4AUVaG-image.png","https://files.peakd.com/file/peakd-hive/june0620/EPOWKLgx-image.png","https://files.peakd.com/file/peakd-hive/june0620/59zaWPQD-image.png"],"links":["https://steemit.com/hive-101145/@june0620/python-10-django-3-steem","https://getbootstrap.com/docs/4.0/examples/"],"tags":["cn","whalepower","dblog","python","palnet","django","goorm"],"users":["june0620"]}
created2020-08-10 13:48:54
last_update2020-08-10 16:59:21
depth0
children2
last_payout2020-08-17 13:48:54
cashout_time1969-12-31 23:59:59
total_payout_value4.931 HBD
curator_payout_value4.360 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,657
author_reputation118,592,211,436,406
root_title"[Python #11] [Django #4] 听说 Bootstrap 可以很容易给网页穿样式? "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,995,518
net_rshares21,884,513,500,966
author_curate_reward""
vote details (66)
@chitty ·
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
properties (22)
authorchitty
permlinkre-python-11-django-4-bootstrap-qeup1c-20200815t000423
categoryhive-105017
json_metadata""
created2020-08-15 00:04:24
last_update2020-08-15 00:04:24
depth1
children1
last_payout2020-08-22 00:04:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation86,901,300,608,582
root_title"[Python #11] [Django #4] 听说 Bootstrap 可以很容易给网页穿样式? "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,076,471
net_rshares0
@june0620 · (edited)
Thank you so much @chitty😃
properties (22)
authorjune0620
permlinkre-chitty-qf33di
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2020.08.3"}
created2020-08-15 02:39:21
last_update2020-08-15 02:40:39
depth2
children0
last_payout2020-08-22 02:39:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length26
author_reputation118,592,211,436,406
root_title"[Python #11] [Django #4] 听说 Bootstrap 可以很容易给网页穿样式? "
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,077,988
net_rshares0