[Python #10] [Django #3] 获取我的POST列表并显示在网页 by june0620

View this thread on steempeak.com
· @june0620 ·
$22.12
[Python #10] [Django #3] 获取我的POST列表并显示在网页
pixabay
https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png

[上期](https://peakd.com/hive-105017/@june0620/python-9-django-2-steem-python-steem)简单的尝试了下把我的STEEM信息投放到网页,今天把我的POSTS放到网页上吧。(#边学边写真难)

##### 添加 URL
首先在blog目录下的 `urls.py` 添加相关url。用`slug`来判断并读取`Views.py`的posts类,用.as_view()来显示ListView。
```
from django.urls import path
from .views import main_view, posts

urlpatterns = [
    path('', main_view, name='my_home'),
    path('@<slug:account>/', posts.as_view(), name='posts'),
]
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/Ohk3u4QJ-image.png)

##### 添加类到`Views.py`
因上面命名了post,所以在 `Views.py`文件加上相应的类,这个类继承django的ListView。
重定义get函数,这里我们用到 `Services.py`里的 blog 函数。
```
from .services import my_data, blogs

class posts(ListView):
    template_name = 'posts.html'
    context_object_name = 'all_posts'

    def get(self, request, *args, **kwargs):
        self.queryset = blogs(kwargs['account'])
        return super().get(request, *args, **kwargs)

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

##### 获取 POST 列表
用 steem-python 库调用某人的post列表,填入到 `Services.py`。
```
from steem import Steem

def blogs(account=str):
    s = Steem()
    blogs = s.get_blog(account, entry_id=0, limit=50)

    return blogs
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/VXI3eDKg-image.png)

##### 创建模板
该创建网页模板了,创建一个叫 `templates` 的文件夹到 `blog` 文件夹同级别下,并在之下创建一个 `posts.html`网页文件。
```
$ mkdir templates
$ touch templates/home.html
```
之后在html文件写入如下:
```
<h1>My Steem Blog</h1>
    <ul>
        {% for post in all_posts %}
            <li><a href="https://www.steemit.com/@{{post.comment.author}}/{{ post.comment.permlink }}" target="_blank">{{ post.comment.title }}</a></li>
        {% endfor %}
    </ul>
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/2VB8d6zy-image.png)
意思就是从 `Views.py` 获取的目录反复用li标签渲染到页面。
还差一步,需要把 template 路径设置到 `Settings.py`。
```
TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]
```
![image.png](https://files.peakd.com/file/peakd-hive/june0620/v8wqyF9T-image.png)

##### 运行
运行本地服务器后在 URL 加 `@june0620`后缀访问,成功。
![image.png](https://files.peakd.com/file/peakd-hive/june0620/moj4HSo6-image.png)

.
.
.
.
[Cookie 😅]
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 70 others
properties (23)
post_id86,671,892
authorjune0620
permlinkpython-10-django-3-post
categoryhive-101145
json_metadata{"tags":["hive-101145","cn","cn-stem","steemstem","mini","zzan","sct-cn","sct-freeboard","dblog","sct"],"image":["https:\/\/cdn.pixabay.com\/photo\/2019\/05\/14\/17\/07\/web-development-4202909_1280.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/Ohk3u4QJ-image.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/3wcNGQdX-image.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/VXI3eDKg-image.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/2VB8d6zy-image.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/v8wqyF9T-image.png","https:\/\/files.peakd.com\/file\/peakd-hive\/june0620\/moj4HSo6-image.png"],"links":["https:\/\/peakd.com\/hive-105017\/@june0620\/python-9-django-2-steem-python-steem"],"app":"steemcoinpan\/0.1","format":"markdown","canonical_url":"https:\/\/www.steemcoinpan.com\/@june0620\/python-10-django-3-post"}
created2020-07-12 13:06:42
last_update2020-07-12 13:06:42
depth0
children1
net_rshares33,784,273,813,726
last_payout2020-07-19 13:06:42
cashout_time1969-12-31 23:59:59
total_payout_value11.496 SBD
curator_payout_value10.619 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2,355
author_reputation219,336,602,045,061
root_title"[Python #10] [Django #3] 获取我的POST列表并显示在网页"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (134)
@sct.krwp ·
@june0620 transfered 1.0 KRWP to @krwp.burn. voting percent : 0.65%, voting power : 79.42%, steem power : 1847324.24, STU KRW : 1200. 
@june0620 staking status : 50 KRWP
                                @june0620 limit for KRWP voting service : 0.1 KRWP (rate : 0.002)
                                What you sent : 1.0 KRWP
                                Refund balance : 0.9 KRWP [45041551 - 259196e521b115f970aeb7f048adf6b0eecb757d]
properties (22)
post_id86,671,933
authorsct.krwp
permlinkre--python-10-django-3-post-20200712t130930262z
categoryhive-101145
json_metadata{"tags":["hive-101145","cn","cn-stem","steemstem","mini","zzan","sct-cn","sct-freeboard","dblog","sct"]}
created2020-07-12 13:09:33
last_update2020-07-12 13:09:33
depth1
children0
net_rshares0
last_payout2020-07-19 13:09:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length436
author_reputation10,000,000,000,000
root_title"[Python #10] [Django #3] 获取我的POST列表并显示在网页"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000