Final Blog website

This commit is contained in:
Mohammad Rameen 2025-04-12 15:37:03 +05:30
parent 4b6ac31c11
commit e893a09549
10 changed files with 136 additions and 44 deletions

View File

@ -0,0 +1,37 @@
from flask import Flask, render_template
import random
import datetime
import requests
from post import Post
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 home():
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)

View File

@ -0,0 +1,6 @@
class Post:
def __init__(self, post_id, title, subtitle, body):
self.id = post_id
self.title = title
self.subtitle = subtitle
self.body = body

View File

@ -1,35 +0,0 @@
from flask import Flask, render_template
import random
import datetime
import requests
app = Flask(__name__)
@app.route('/')
def home():
random_number = random.randint(1, 10)
current_year = datetime.datetime.now().year
return render_template('index.html', num=random_number, year=current_year)
@app.route('/guess/<name>')
def guess(name):
gender_url = f"https://api.genderize.io?name={name}"
age_url = f"https://api.agify.io?name={name}"
gender_response = requests.get(gender_url)
gender_data = gender_response.json()
gender = gender_data["gender"]
age_response = requests.get(age_url)
age_data = age_response.json()
age = age_data["age"]
return render_template('guess.html', name=name, gender=gender, age=age)
@app.route('/blog')
def get_blog():
blog_url = "https://api.npoint.io/c790b4d5cab58020d391"
blog_response = requests.get(blog_url)
all_posts = blog_response.json()
return render_template("blog.html", posts=all_posts)
if __name__ == "__main__":
app.run(debug=True)

View File

@ -0,0 +1,71 @@
body{
background: #efeff3;
margin: 0;
font-family: 'Raleway', sans-serif;
-webkit-font-smoothing: antialiased;
color:#212121;
}
.wrapper{
position: relative;
clear:both;
margin: 0 auto 75px auto;
width: 100%;
overflow: hidden;
}
.top{
background: #4e89ae;
height: 180px;
border-top: 20px solid #43658b;
}
.top .title {
width: 700px;
margin: 38px auto 0 auto;
}
.title h1 {
font-size:24px;
color:#FFF;
font-weight:500;
}
.content{
margin: -80px auto 100px;
padding-bottom: 20px;
}
.card{
position: relative;
background: #fff;
padding:50px;
width: 600px;
margin: 20px auto 0 auto;
box-shadow: 0 2px 4px rgba(100,100,100,.1);
}
.card h2 {
font-size:21px;
font-weight:500;
}
.card h2 a {
color:#CC0000;
text-decoration:none;
}
.card .text {
color:#212121;
margin-top:20px;
font-size:15px;
line-height:22px;
}
footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: #43658b;
color: white;
text-align: center;
}

View File

@ -1,16 +1,29 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF8"> <meta charset="UTF-8">
<title>My Website</title> <title>Title</title>
<link href="https://fonts.googleapis.com/css2?family=Raleway" rel="stylesheet">
<link rel="stylesheet" href="../static/css/styles.css">
</head> </head>
<body> <body>
<h1>Hello World!</h1> <div class="wrapper">
<h2>{{ 5 * 6}}</h2> <div class="top">
<h3>Random Number: {{ num }}</h3> <div class="title"><h1>My Blog</h1></div>
<a href="{{ url_for('get_blog') }}">Go To Blog</a> </div>
{% for post in all_posts: %}
<div class="content">
<div class="card">
<h2>{{ post.title }}</h2>
<p class="text">{{ post.subtitle }}</p>
<a href="{{ url_for('show_post', index=post.id) }}">Read</a>
</div>
</div>
{% endfor %}
</div>
</body> </body>
<footer> <footer>
<p>Copyright {{ year }}. Built by Mohammad.</p> <p>Made with ♥️ in India.</p>
</footer> </footer>
</html> </html>