Cache the default rendered home page
A bit hacky, since CLForm can't seem to initialize properly without being part of a request. To avoid a reference-before-assignment error, the INDEX variable is first initialized as a global in __init__.py. But it is definitely faster. Repeated renders take around 2ms each. Reading the cached value takes about 0.002ms. Also split index() into index_get() and index_post(), since GET calls always use the cached version, and POST calls never do.
This commit is contained in:
parent
e52a0e9d28
commit
7398773714
|
@ -10,6 +10,8 @@ from flask import (
|
|||
|
||||
import writing
|
||||
|
||||
INDEX = None
|
||||
|
||||
def optimize_css():
|
||||
import re
|
||||
root = os.path.dirname(os.getcwd())
|
||||
|
|
16
writing.py
16
writing.py
|
@ -5,6 +5,7 @@ from wtforms import Form, BooleanField, StringField, TextAreaField, validators
|
|||
import urllib.parse
|
||||
|
||||
from latty import CLData
|
||||
import flaskr
|
||||
|
||||
|
||||
writing_blueprint = Blueprint('writing', __name__,)
|
||||
|
@ -47,10 +48,19 @@ class CLForm(Form):
|
|||
default=body_string
|
||||
)
|
||||
|
||||
@writing_blueprint.route('/', methods=('GET', 'POST'))
|
||||
def index():
|
||||
@writing_blueprint.route('/', methods=['GET'])
|
||||
def index_get():
|
||||
if flaskr.INDEX == None:
|
||||
flaskr.INDEX = render_template(
|
||||
'writing.html',
|
||||
form=CLForm()
|
||||
)
|
||||
return flaskr.INDEX
|
||||
|
||||
@writing_blueprint.route('/', methods=['POST'])
|
||||
def index_post():
|
||||
form = CLForm(request.form)
|
||||
if request.method == 'POST' and form.validate():
|
||||
if form.validate():
|
||||
data = CLData(
|
||||
username=form.username.data,
|
||||
company=form.company.data,
|
||||
|
|
Loading…
Reference in New Issue