Skip to content

Commit b6cff5b

Browse files
committed
Flask Series Code
1 parent c325490 commit b6cff5b

File tree

210 files changed

+6939
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

210 files changed

+6939
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from flask import Flask
2+
app = Flask(__name__)
3+
4+
5+
@app.route("/")
6+
@app.route("/home")
7+
def home():
8+
return "<h1>Home Page</h1>"
9+
10+
11+
@app.route("/about")
12+
def about():
13+
return "<h1>About Page</h1>"
14+
15+
16+
if __name__ == '__main__':
17+
app.run(debug=True)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from flask import Flask, render_template, url_for
2+
app = Flask(__name__)
3+
4+
posts = [
5+
{
6+
'author': 'Corey Schafer',
7+
'title': 'Blog Post 1',
8+
'content': 'First post content',
9+
'date_posted': 'April 20, 2018'
10+
},
11+
{
12+
'author': 'Jane Doe',
13+
'title': 'Blog Post 2',
14+
'content': 'Second post content',
15+
'date_posted': 'April 21, 2018'
16+
}
17+
]
18+
19+
20+
@app.route("/")
21+
@app.route("/home")
22+
def home():
23+
return render_template('home.html', posts=posts)
24+
25+
26+
@app.route("/about")
27+
def about():
28+
return render_template('about.html', title='About')
29+
30+
31+
if __name__ == '__main__':
32+
app.run(debug=True)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
body {
2+
background: #fafafa;
3+
color: #333333;
4+
margin-top: 5rem;
5+
}
6+
7+
h1, h2, h3, h4, h5, h6 {
8+
color: #444444;
9+
}
10+
11+
.bg-steel {
12+
background-color: #5f788a;
13+
}
14+
15+
.site-header .navbar-nav .nav-link {
16+
color: #cbd5db;
17+
}
18+
19+
.site-header .navbar-nav .nav-link:hover {
20+
color: #ffffff;
21+
}
22+
23+
.site-header .navbar-nav .nav-link.active {
24+
font-weight: 500;
25+
}
26+
27+
.content-section {
28+
background: #ffffff;
29+
padding: 10px 20px;
30+
border: 1px solid #dddddd;
31+
border-radius: 3px;
32+
margin-bottom: 20px;
33+
}
34+
35+
.article-title {
36+
color: #444444;
37+
}
38+
39+
a.article-title:hover {
40+
color: #428bca;
41+
text-decoration: none;
42+
}
43+
44+
.article-content {
45+
white-space: pre-line;
46+
}
47+
48+
.article-img {
49+
height: 65px;
50+
width: 65px;
51+
margin-right: 16px;
52+
}
53+
54+
.article-metadata {
55+
padding-bottom: 1px;
56+
margin-bottom: 4px;
57+
border-bottom: 1px solid #e3e3e3
58+
}
59+
60+
.article-metadata a:hover {
61+
color: #333;
62+
text-decoration: none;
63+
}
64+
65+
.article-svg {
66+
width: 25px;
67+
height: 25px;
68+
vertical-align: middle;
69+
}
70+
71+
.account-img {
72+
height: 125px;
73+
width: 125px;
74+
margin-right: 20px;
75+
margin-bottom: 16px;
76+
}
77+
78+
.account-heading {
79+
font-size: 2.5rem;
80+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
<h1>About Page</h1>
4+
{% endblock content %}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
{% for post in posts %}
4+
<article class="media content-section">
5+
<div class="media-body">
6+
<div class="article-metadata">
7+
<a class="mr-2" href="#">{{ post.author }}</a>
8+
<small class="text-muted">{{ post.date_posted }}</small>
9+
</div>
10+
<h2><a class="article-title" href="#">{{ post.title }}</a></h2>
11+
<p class="article-content">{{ post.content }}</p>
12+
</div>
13+
</article>
14+
{% endfor %}
15+
{% endblock content %}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
10+
11+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='main.css') }}">
12+
13+
{% if title %}
14+
<title>Flask Blog - {{ title }}</title>
15+
{% else %}
16+
<title>Flask Blog</title>
17+
{% endif %}
18+
</head>
19+
<body>
20+
<header class="site-header">
21+
<nav class="navbar navbar-expand-md navbar-dark bg-steel fixed-top">
22+
<div class="container">
23+
<a class="navbar-brand mr-4" href="/">Flask Blog</a>
24+
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarToggle" aria-controls="navbarToggle" aria-expanded="false" aria-label="Toggle navigation">
25+
<span class="navbar-toggler-icon"></span>
26+
</button>
27+
<div class="collapse navbar-collapse" id="navbarToggle">
28+
<div class="navbar-nav mr-auto">
29+
<a class="nav-item nav-link" href="/">Home</a>
30+
<a class="nav-item nav-link" href="/about">About</a>
31+
</div>
32+
<!-- Navbar Right Side -->
33+
<div class="navbar-nav">
34+
<a class="nav-item nav-link" href="/login">Login</a>
35+
<a class="nav-item nav-link" href="/register">Register</a>
36+
</div>
37+
</div>
38+
</div>
39+
</nav>
40+
</header>
41+
<main role="main" class="container">
42+
<div class="row">
43+
<div class="col-md-8">
44+
{% block content %}{% endblock %}
45+
</div>
46+
<div class="col-md-4">
47+
<div class="content-section">
48+
<h3>Our Sidebar</h3>
49+
<p class='text-muted'>You can put any information here you'd like.
50+
<ul class="list-group">
51+
<li class="list-group-item list-group-item-light">Latest Posts</li>
52+
<li class="list-group-item list-group-item-light">Announcements</li>
53+
<li class="list-group-item list-group-item-light">Calendars</li>
54+
<li class="list-group-item list-group-item-light">etc</li>
55+
</ul>
56+
</p>
57+
</div>
58+
</div>
59+
</div>
60+
</main>
61+
62+
63+
<!-- Optional JavaScript -->
64+
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
65+
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
66+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
67+
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
68+
</body>
69+
</html>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from flask import Flask, render_template, url_for, flash, redirect
2+
from forms import RegistrationForm, LoginForm
3+
4+
app = Flask(__name__)
5+
app.config['SECRET_KEY'] = '5791628bb0b13ce0c676dfde280ba245'
6+
7+
posts = [
8+
{
9+
'author': 'Corey Schafer',
10+
'title': 'Blog Post 1',
11+
'content': 'First post content',
12+
'date_posted': 'April 20, 2018'
13+
},
14+
{
15+
'author': 'Jane Doe',
16+
'title': 'Blog Post 2',
17+
'content': 'Second post content',
18+
'date_posted': 'April 21, 2018'
19+
}
20+
]
21+
22+
23+
@app.route("/")
24+
@app.route("/home")
25+
def home():
26+
return render_template('home.html', posts=posts)
27+
28+
29+
@app.route("/about")
30+
def about():
31+
return render_template('about.html', title='About')
32+
33+
34+
@app.route("/register", methods=['GET', 'POST'])
35+
def register():
36+
form = RegistrationForm()
37+
if form.validate_on_submit():
38+
flash(f'Account created for {form.username.data}!', 'success')
39+
return redirect(url_for('home'))
40+
return render_template('register.html', title='Register', form=form)
41+
42+
43+
@app.route("/login", methods=['GET', 'POST'])
44+
def login():
45+
form = LoginForm()
46+
if form.validate_on_submit():
47+
if form.email.data == 'admin@blog.com' and form.password.data == 'password':
48+
flash('You have been logged in!', 'success')
49+
return redirect(url_for('home'))
50+
else:
51+
flash('Login Unsuccessful. Please check username and password', 'danger')
52+
return render_template('login.html', title='Login', form=form)
53+
54+
55+
if __name__ == '__main__':
56+
app.run(debug=True)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import StringField, PasswordField, SubmitField, BooleanField
3+
from wtforms.validators import DataRequired, Length, Email, EqualTo
4+
5+
6+
class RegistrationForm(FlaskForm):
7+
username = StringField('Username',
8+
validators=[DataRequired(), Length(min=2, max=20)])
9+
email = StringField('Email',
10+
validators=[DataRequired(), Email()])
11+
password = PasswordField('Password', validators=[DataRequired()])
12+
confirm_password = PasswordField('Confirm Password',
13+
validators=[DataRequired(), EqualTo('password')])
14+
submit = SubmitField('Sign Up')
15+
16+
17+
class LoginForm(FlaskForm):
18+
email = StringField('Email',
19+
validators=[DataRequired(), Email()])
20+
password = PasswordField('Password', validators=[DataRequired()])
21+
remember = BooleanField('Remember Me')
22+
submit = SubmitField('Login')
Binary file not shown.
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
body {
2+
background: #fafafa;
3+
color: #333333;
4+
margin-top: 5rem;
5+
}
6+
7+
h1, h2, h3, h4, h5, h6 {
8+
color: #444444;
9+
}
10+
11+
.bg-steel {
12+
background-color: #5f788a;
13+
}
14+
15+
.site-header .navbar-nav .nav-link {
16+
color: #cbd5db;
17+
}
18+
19+
.site-header .navbar-nav .nav-link:hover {
20+
color: #ffffff;
21+
}
22+
23+
.site-header .navbar-nav .nav-link.active {
24+
font-weight: 500;
25+
}
26+
27+
.content-section {
28+
background: #ffffff;
29+
padding: 10px 20px;
30+
border: 1px solid #dddddd;
31+
border-radius: 3px;
32+
margin-bottom: 20px;
33+
}
34+
35+
.article-title {
36+
color: #444444;
37+
}
38+
39+
a.article-title:hover {
40+
color: #428bca;
41+
text-decoration: none;
42+
}
43+
44+
.article-content {
45+
white-space: pre-line;
46+
}
47+
48+
.article-img {
49+
height: 65px;
50+
width: 65px;
51+
margin-right: 16px;
52+
}
53+
54+
.article-metadata {
55+
padding-bottom: 1px;
56+
margin-bottom: 4px;
57+
border-bottom: 1px solid #e3e3e3
58+
}
59+
60+
.article-metadata a:hover {
61+
color: #333;
62+
text-decoration: none;
63+
}
64+
65+
.article-svg {
66+
width: 25px;
67+
height: 25px;
68+
vertical-align: middle;
69+
}
70+
71+
.account-img {
72+
height: 125px;
73+
width: 125px;
74+
margin-right: 20px;
75+
margin-bottom: 16px;
76+
}
77+
78+
.account-heading {
79+
font-size: 2.5rem;
80+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
<h1>About Page</h1>
4+
{% endblock content %}

0 commit comments

Comments
 (0)