forked from elmiram/2016learnpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_app.py
More file actions
61 lines (45 loc) · 1.74 KB
/
my_app.py
File metadata and controls
61 lines (45 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import datetime
from flask import Flask
from flask import url_for, render_template, request, redirect
app = Flask(__name__)
@app.route('/')
def index():
urls = {'главная (эта страница)': url_for('index'),
'привет (переменные в url)': url_for('hi'),
'форма (форма и ответ на одном url)': url_for('form'),
'форма про книги (просто форма)': url_for('books'),
'спасибо (попадаем сюда только после формы про книги)': url_for('thanks'),
'время (используем redirect)': url_for('time_redirect'),}
return render_template('index.html', urls=urls)
@app.route('/hi')
@app.route('/hi/<user>')
def hi(user=None):
if user is None:
user = 'friend'
return '<html><body><p>Привет, ' + user + '!</p></body></html>'
@app.route('/form')
def form():
if request.args:
name = request.args['name']
age = request.args['age']
st = True if 'student' in request.args else False
return render_template('answer.html', name=name, age=age, student=st)
return render_template('question.html')
@app.route('/books')
def books():
return render_template('books.html')
@app.route('/thanks')
def thanks():
if request.args:
name = request.args['name']
book = request.args['book']
return render_template('thanks.html', name=name, book=book)
return redirect(url_for('books'))
@app.route('/time')
def time_redirect():
h = datetime.datetime.today().hour
if 10 < h < 18:
return redirect(url_for('index'))
return redirect(url_for('hi'))
if __name__ == '__main__':
app.run(debug=True)