Database Reference
In-Depth Information
Login Form
The HTML required for the user login form is shown in Listing 9-17 and can be found in the {PROJECTROOT}/app/
public/templates/global/base-home.html layout file.
Listing 9-17. The Login Form
<form class="navbar-form navbar-right" action="/login" role="form" method="post">
<div class="form-group">
<input type="text" placeholder="Username" name="username" class="form-control">
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
Login Route
In the graphstory application, use the login route to control the flow of the login process, as shown in Listing 9-18.
Inside the login route, use the get_user_by_username method to check if the user exists in the database.
Listing 9-18. The login Route
@route('/login', method='POST')
def login():
# make sure username was passed
username = request.forms.get('username').strip().lower()
if username:
# look for username
user = User().get_user_by_username(graph_db, username)
if user:
# user found, set cookie and redirect
response.set_cookie(graphstoryUserAuthKey, user["username"], path="/")
redirect("/social")
else:
# otherwise send back with not found message
return template("public/templates/home/index.html", layout=homelayout, title="Home",
error="The username you entered was not found.")
# otherwise send back
else:
return template('public/templates/home/index.html', layout=homelayout, title="Home",
error="Please enter a username.")
If the user is found during the login attempt, a cookie is added to the response and the request is redirected via
redirect the social home page, shown in Figure 9-7 . Otherwise, the route will specify the HTML page to return and
will add the error messages that need to be displayed back to the view.
Search WWH ::




Custom Search