Thank you for your help over the past few days. I am not there yet. So I have decided to create a minimal application based on the sample in chapter five of the manual.
The application should have one text field, and one button. Whenever the button is pressed the string '1' should be concatenated the value in the field and a flash should advertise this.
The code as it stands simply displays "None" in the field.
The form is a simplified version of the show_entries template:
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<form action="{{ url_for('add_entry') }}" method="post" class="add-entry">
<h2> Integer:</h2>
<input id="text" name="text" size="10" value="{{ AO_sInteger }}" />
<input type="submit" value="Add 1" />
</form>
{% endif %}
{% endblock %}
The "minimal" app is:
import sqlite3
from werkzeug.wrappers import Request, Response
from jinja2 import Template
from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flash, make_response
app = Flask(__name__)
app.config.from_object(__name__)
app.config.from_envvar('FLASKR_SETTINGS', silent=True)
app.secret_key = '\xa5M\x05\xda=Y<\xfdV\x1f#\xa6\\\xbd%\xd8\xa1mBd\xca\xc9\xb1\xfe'
app.debug = True
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
AO_sInteger = request.form['text']
AO_sInteger = AO_sInteger+'1'
render_template('show_entries.html', AO_sInteger = AO_sInteger)
resp = make_response(render_template('show_entries.html', AO_sInteger = AO_sInteger))
resp.set_cookie('AO_sInteger', AO_sInteger)
flash('the new seed is: %s.' %(session.get('AO_sInteger')))
return redirect(url_for('show_entries'))
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
session['logged_in'] = True
flash('You were logged in')
return redirect(url_for('show_entries'))
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('show_entries'))
@app.route('/', methods=['GET', 'POST'])
def show_entries():
try:
AO_sInteger = request.cookies.get('AO_sInteger')
except KeyError:
AO_sInteger = '42'
return render_template('show_entries.html', AO_sInteger = session.get('AO_sInteger'))
if __name__ == '__main__':
app.run()
I would have expected to have 42 as the initial display when the cookie was not created . That shows that I do not understand the invocation order of the events in the program.
Thanks, and sorry for the long post.
Avner
You are mixing up the session (which by default uses signed cookies to store the session data in) with your own custom cookies. More specifically, in your show_entries code you get the cookie AO_sInteger but then pass in the value pulled from session['AO_sInteger'] (which you haven't set anywhere). You'll want to use one or the other consistently.
For example, changing the last line of show_entries to use AO_sInteger rather than session.get('AO_sInteger)` would fix the problem.
I don't know why you want to use cookies here but It seems that your confuse about session and cookies (It's difference things). You should use session here
If you want to set default value to AO_sInteger, your code can rewrite to something like:
@app.route('/add', methods=['POST'])
def add_entry():
if not session.get('logged_in'):
abort(401)
session['AO_sInteger'] = request.form.get('text', 41) + 1
flash('the new seed is: %s.' %(session.get('AO_sInteger')))
return redirect(url_for('show_entries'))
@app.route('/', methods=['GET', 'POST'])
def show_entries():
return render_template('show_entries.html', AO_sInteger = session.get('AO_sInteger', 42))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With