[Python #10] [Django #3] STEEM 포스트 목록 웹페이지 노출
지난번에 이어, 오늘은 스팀에 등록된 내 포스팅을 불러와 웹페이지에 뿌려본다.
URL 추가
우선, blog 디렉터리 내 urls.py
에 관련 URL을 추가한다. slug
URL의 계정을 판단 후 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'),
]
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)
POST 목록 불러오기
steem-python 모듈로 특정 사용자의 목록을 불러오는 함수를 Services.py
에 구성한다.
from steem import Steem
def blogs(account=str):
s = Steem()
blogs = s.get_blog(account, entry_id=0, limit=50)
return blogs
템플릿 생성
이제 웹페이지의 템플릿을 만들어 보자. 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>
기본 HTML 문법이 아닌데... 대충 Views.py
에서 불러온 포스팅 목록을 반복문으로 li 태그에 뿌려주는 문법이다.
마지막으로 template 경로를 Settings.py
에 지정해준다.
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
},
]
실행
로컬 서버 실행 후 URL 뒤에 @june0620
를 추가하니 성공이다.
.
.
.
.
[Cookie 😅]
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3
@tipu curate
Upvoted 👌 (Mana: 4/20) @swap.app - quick steem <-> sbd swaps
티퓨 항상 감사합니다^^
@june0620 transfered 1.0 KRWP to @krwp.burn. voting percent : 0.66%, voting power : 79.23%, steem power : 1849776.85, 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 [45098517 - 5b6ec3f1c5b6842d68992edeadc04a358840dd15]
补完拍手😜