0

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>';

1 Answers1

-1

You can use r.keys() to get a list of all the keys in the current redis database. r.keys() also takes in an optional pattern to match against the keys.

So you can use r.keys("*:pregunta") to match all the questions.

Also, the line-end semi-colons are quite unnecessary.

zenofsahil
  • 1,713
  • 2
  • 16
  • 18
  • Please be aware that `KEYS` is an expensive operation and depending on the number of keys in your redis server it may take a long time. Using `SCAN` is preferred here. Example: http://stackoverflow.com/questions/25970315/redis-scan-command-match-option-does-not-work-in-python?answertab=votes#tab-top – tobiash Feb 21 '16 at 03:53