Skip to content

Commit 6215aef

Browse files
Login form added, Templates Updates
1 parent 498ed97 commit 6215aef

11 files changed

+67
-3
lines changed

__pycache__/config.cpython-38.pyc

376 Bytes
Binary file not shown.

app/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from flask import Flask
2+
from config import Config
23

34
app = Flask(__name__)
5+
app.config.from_object(Config)
46

57
from app import routes
68

72 Bytes
Binary file not shown.

app/__pycache__/forms.cpython-38.pyc

684 Bytes
Binary file not shown.

app/__pycache__/routes.cpython-38.pyc

483 Bytes
Binary file not shown.

app/forms.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from flask_wtf import FlaskForm
2+
from wtforms import StringField, BooleanField, SubmitField, PasswordField
3+
from wtforms.validators import DataRequired
4+
5+
class LoginForm(FlaskForm):
6+
username = StringField('Username', validators = [DataRequired()])
7+
password = PasswordField('Password', validators = [DataRequired()])
8+
remember_me = BooleanField('Remember Me')
9+
submit = SubmitField('Sign In')

app/routes.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from flask import render_template
1+
from flask import render_template, flash, redirect,url_for
22
from app import app
3+
from app.forms import LoginForm
34

45
@app.route('/')
56
@app.route('/index')
@@ -16,3 +17,12 @@ def index():
1617
}
1718
]
1819
return render_template('index.html',title = "Home", user = user, posts = posts)
20+
21+
@app.route('/login',methods = ['GET','POST'])
22+
def login():
23+
form = LoginForm()
24+
if form.validate_on_submit():
25+
flash('Login requested for user {}, remember_me ={}'.format(
26+
form.username.data, form.remember_me.data))
27+
return redirect(url_for('index'))
28+
return render_template('login.html',title = 'Sign In',form = form)

app/templates/base.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
<title>{{ title }} - Microblog</title>
44
</head>
55
<body>
6-
<div>Microblog: <a href="/index">Home</a></div>
6+
<div>Microblog:
7+
<a href="{{url_for('index') }}">Home</a>
8+
<a href="{{url_for('login') }}">Sign In</a>
9+
</div>
710
<hr>
11+
{% with messages = get_flashed_messages() %}
12+
{% if messages %}
13+
<ul>
14+
{% for message in messages %}
15+
<li>{{message}}</li>
16+
{% endfor %}
17+
</ul>
18+
{% endif %}
19+
{% endwith %}
820
{% block content %}{% endblock %}
921
</body>
1022
</html>

app/templates/login.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
5+
<h1>Sign In</h1>
6+
<form action = "" method = "post" novalidate>
7+
{{form.hidden_tag()}}
8+
<p>
9+
{{form.username.label}} <br>
10+
{{form.username(size=32)}}<br>
11+
{% for error in form.username.errors %}
12+
<span style = "color : red;">[{{error}}]</span>
13+
{% endfor %}
14+
</p>
15+
<p>
16+
{{form.password.label}} <br>
17+
{{form.password(size=32)}}<br>
18+
{% for error in form.password.errors %}
19+
<span style = "color : red;">[{{error}}]</span>
20+
{% endfor %}
21+
</p>
22+
<p>{{form.remember_me()}} {{form.remember_me.label}}</p>
23+
<p>{{form.submit()}}</p>
24+
25+
</form>
26+
{% endblock %}

config.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
3+
class Config(object):
4+
SECRET_KEY = os.environ.get('SECRET_KEY') or 'hello111'

microblog.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from app import app
1+
from app import app
2+

0 commit comments

Comments
 (0)