36.7. ADDING AUTHORIZATION
89def login(request):
90login_url = request.resource_url(request.context, ’login’)
91referrer = request.url
92if referrer == login_url:
93referrer = ’/’ # never use the login form itself as came_from
94came_from = request.params.get(’came_from’, referrer)
95message = ’’
96login = ’’
97password = ’’
98if ’form.submitted’ in request.params:
99login = request.params[’login’]
100password = request.params[’password’]
101if USERS.get(login) == password:
102 |
headers = remember(request, |
login) |
103 |
return HTTPFound(location = |
came_from, |
104 |
headers = headers) |
105 |
message = ’Failed login’ |
|
106
107return dict(
108message = message,
109url = request.application_url + ’/login’,
110came_from = came_from,
111login = login,
112password = password,
113)
114
115@view_config(context=’.models.Wiki’, name=’logout’)
116def logout(request):
117headers = forget(request)
118return HTTPFound(location = request.resource_url(request.context),
Our edit.pt template will look something like this when we’re done:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" |
4 |
xmlns:tal="http://xml.zope.org/namespaces/tal"> |
5 |
<head> |
6 |
<title>${page.__name__} - Pyramid tutorial wiki (based on |
7TurboGears 20-Minute Wiki)</title>
8 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
9<meta name="keywords" content="python web application" />
10<meta name="description" content="pyramid web application" />
11<link rel="shortcut icon"
12href="/static/favicon.ico" />