-
-
Save TheMuellenator/7c6a08a3df3b94a28d1a867628481910 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet"> | |
<link rel="stylesheet" href="../static/css/styles.css"> | |
</head> | |
<body> | |
<div class="wrapper"> | |
<div class="top"> | |
<div class="title"><h1>My Blog</h1></div> | |
</div> | |
{% for post in all_posts: %} | |
<div class="content"> | |
<div class="card "> | |
<h2>{{ post.title }}</h2> | |
<p>{{ post.subtitle }}</p> | |
<a href="{{ url_for('show_post', index=post.id) }}">Read</a> | |
</div> | |
</div> | |
{% endfor %} | |
</div> | |
</body> | |
<footer> | |
<p>Made with ♥️ in London.</p> | |
</footer> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template | |
from post import Post | |
import requests | |
posts = requests.get("https://api.npoint.io/c790b4d5cab58020d391").json() | |
post_objects = [] | |
for post in posts: | |
post_obj = Post(post["id"], post["title"], post["subtitle"], post["body"]) | |
post_objects.append(post_obj) | |
app = Flask(__name__) | |
@app.route('/') | |
def get_all_posts(): | |
return render_template("index.html", all_posts=post_objects) | |
@app.route("/post/<int:index>") | |
def show_post(index): | |
requested_post = None | |
for blog_post in post_objects: | |
if blog_post.id == index: | |
requested_post = blog_post | |
return render_template("post.html", post=requested_post) | |
if __name__ == "__main__": | |
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet"> | |
<link rel="stylesheet" href="../static/css/styles.css"> | |
</head> | |
<body> | |
<div class="wrapper"> | |
<div class="top"> | |
<div class="title"><h1>My Blog</h1></div> | |
</div> | |
<div class="content"> | |
<div class="card"> | |
<h1> {{ post.title }}</h1> | |
<h2> {{ post.subtitle }}</h2> | |
<p> {{ post.body }}</p> | |
</div> | |
</div> | |
</div> | |
</body> | |
<footer> | |
<p>Made with ♥️ in London.</p> | |
</footer> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post: | |
def __init__(self, post_id, title, subtitle, body): | |
self.id = post_id | |
self.title = title | |
self.subtitle = subtitle | |
self.body = body |
main.py
from flask import Flask, render_template from post import Post app = Flask(__name__) @app.route('/') def home(): blogs = Post() return render_template("index.html", posts=blogs.posts) @app.route('/post/<int:id>') def get_post(id): blogs = Post() post = blogs.get_post(id) return render_template("post.html", title=post['title'], body=post['body']) if __name__ == "__main__": app.run(debug=True)post.py
import requests class Post: def __init__(self): blog_url = "https://api.npoint.io/c790b4d5cab58020d391" req = requests.get(blog_url) all_posts = req.json() self.posts = all_posts def get_post(self, id): for post in self.posts: if post['id'] == id: return postindex.html
{% for post in posts: %} <div class="content"> <div class="card"> <h2>{{ post['title'] }}</h2> <p class="text">{{ post['subtitle'] }}</p> <a href="{{ url_for('get_post', id=post['id'])}}">Read</a> </div> </div> {% endfor %}
post.html
<div class="content"> <div class="card"> <h2>{{ title }}</h2> <p>{{ body }}</p> </div> </div>
This is exactly what I was trying to do.
Thanks. Nice Algorithm
### main.py
from flask import Flask, render_template
from post import Post
app = Flask(name)
c_post = Post()
@app.route('/')
def home():
posts = c_post.all_blog_post()
return render_template("index.html", posts=posts)
@app.route('/post/int:blog_id')
def read_blog(blog_id):
blog_post = c_post.ind_post(blog_id)
return render_template("post.html", blog_post=blog_post)
if name == "main":
app.run(debug=True)
### post.py
import requests
class Post:
def init(self):
self.posts = []
def all_blog_post(self):
url = "https://api.npoint.io/c790b4d5cab58020d391"
self.posts = requests.get(url).json()
return self.posts
def ind_post(self, post_id):
all_post_details = self.all_blog_post()
post = []
for p in all_post_details:
if p["id"] == post_id:
post.append(p)
return post
index.html
<title>Title</title>My Blog
Made with
post.html
<title>Title</title>My Blog
{{ blog_post[0]['title'] }}
{{ blog_post[0]['subtitle'] }}
{{ blog_post[0]['body'] }}
</div>
Made with
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
main.py
post.py
index.html
post.html