I am learning trying to learn python flask + redis, then I am doing a basic app where I can make a question with answer and after someone can give me a answer to one of the question, and work, but I wish print out all the questions, but I don't get those, try making a for loop but it no work then I try with r.lrange
but it doesn't works to me, maybe someone can explain how it's work, look this is my
Router.py
r = redis.StrictRedis(host='localhost',port=6379,db=0, charset="utf-8", decode_responses=True);
# Alternativas
# r = redis.StrictRedis();
# r = redis.StrictRedis('localhost',6379,0);
# Server/
@app.route('/')
def hello():
crearLink = "<a href='" + url_for('crear') + "'>Hacer una pregunta</a>";
return """<html>
<head>
<title>Hola, Mundo!</title>
</head>
<body>
""" + crearLink +"""
</body>
</html>""";
# Server/crear
@app.route('/crear', methods=['GET', 'POST'])
def crear():
if request.method == 'GET':
# enviar el formulario al usuario
return render_template('CreateQuestion.html');
elif request.method == 'POST':
# leer la informacion del form y guardarla
titulo = request.form['titulo'];
pregunta = request.form['pregunta'];
respuesta = request.form['respuesta'];
# Guardar informacion
# Key name
r.set(titulo +':pregunta', pregunta);
r.set(titulo +':respuesta', respuesta);
return render_template('CreatedQuestion.html', pregunta = pregunta);
else:
return "<h2>Error</h2>";
def preguntas():
preguntass = r.lrange('pregunta','0','4')
print preguntass;
# server/pregunta/<title>
@app.route('/pregunta/<titulo>', methods=['GET', 'POST'])
def pregunta(titulo):
if request.method == 'GET':
# Leer la pregunta de la base de datos
pregunta = r.get(titulo+':pregunta')
return render_template('AnswerQuestion.html', pregunta = pregunta);
elif request.method == 'POST':
submittedAnswer = request.form['submittedAnswer'];
if submittedAnswer == '':
return "No ingresastes ninguna clase de informacion para validar.";
respuesta = r.get(titulo+':respuesta')
if submittedAnswer == respuesta:
return render_template('Correct.html');
else:
return render_template('Incorrect.html', submittedAnswer = submittedAnswer, respuesta = respuesta);
else:
return '<h2>Error</h2>';