Creating Single Post Template
#index.py
from flask import render_template
from flask_classful import FlaskView, route
from models.postdb import Postdb
class Index(FlaskView):
@route('/')
def index(self):
self.postdb = Postdb()
vdict = self.postdb.get_post()
return render_template('index.html', data=vdict)
@route('/post//')
def get_single_post(self, id):
self.postdb = Postdb()
vdict = self.postdb.get_post(id)
return render_template('post.html', data=vdict)
#models/post.py
import config, copy, lib, requests
from flask import render_template
from flask_classful import FlaskView, route
class Postdb():
def get_post(self, id=''):
vdict = copy.deepcopy(config.vdict)
if id:
URL = "https://www.googleapis.com/blogger/v3/blogs/"+vdict['blog-id']+"/posts/"+id
PARAMS = {"key":vdict['api-key']}
r = requests.get(url = URL, params = PARAMS)
data = r.json()
vdict['post'] = data
self.vlib = lib.Lib()
vdict['date'] = self.vlib.set_date()
return vdict
<!--templates/index.html-->
{% extends "base.html" %}
{% block head %}
{{ super() }}
<script src="/static/scripts/post.js"></script>
<link href="/static/styles/post.css" rel="stylesheet">
{% endblock %}
{% block ad %}
<div class="ad region">
<img src="/static/images/ad.jpg" />
</div>
<div style="clear: both;"></div>
{% endblock %}
{% block content %}
<div id='content'>
{% if 'post' in data %}
<h3 class="post-title">{{ data['post']['title'] }}</h3>
<div class="meta">
<span class="author">អ្នកចុះផ្សាយៈ {{ data['post']['author']['displayName'] }}</span>
<span class="date">
<script>
$('#content .date').append(post.toKhDate("{{ data['post']['published'] }}"));
</script>
</span>
</div>
<div class="post-content">{{ data['post']['content']|safe }}</div>
{% endif %}
<div style="margin-top:20px;" id="disqus_thread"></div>
<script>
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://khmerweb.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
{% endblock %}
/* /static/styles/post.css */
.ad img{
width: 100%;
margin: 20px 0 0;
}
#content{
background: lavender;
padding: 20px;
}
#content .post-title{
font: 22px/1.5 Oswald, Limonf3;
}
#content .meta{
margin: 20px 0;
}
#content .meta .date{
float: right;
}
#content .post-content{
font: 17px/1.75 Courgette, HandWriting;
font-weight: 600;
color: rgb(77, 77, 77)
}
#content .post-content img{
width: 100%;
}
GitHub: "https://github.com/Sokhavuth/newspaper
Heroku: https://khmerweb-newspaper.herokuapp.com/

Comments
Post a Comment