python - 如何打印键redis的所有值

标签 python flask redis

我正在学习尝试学习 python flask + redis,然后我正在做一个基本的应用程序,我可以在其中提出一个问题和答案,然后有人可以给我一个问题的答案,然后工作,但我希望打印解决所有问题,但我不明白,尝试制作一个 for 循环但它不起作用然后我尝试使用 r.lrange 但它对我不起作用,也许有人可以解释如何它的工作,看这是我的 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>';

最佳答案

您可以使用r.keys() 获取当前redis 数据库中所有键的列表。 r.keys() 还采用可选模式来匹配键。

所以你可以使用r.keys("*:pregunta")来匹配所有的问题。

另外,行尾的分号是完全没有必要的。

关于python - 如何打印键redis的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517808/

相关文章:

python - 使用 virtualenv 和 flask 运行 uwsgi

python - 使用 BeautifulSoup 查找 href 链接

python - 尝试在heroku上使用flask python将数据插入postgres时出错

Python & Flask 错误使用 Flask-MySQL |未找到模块错误 : No module named 'flask_mysqldb'

c# - StackExchange.Redis ListRightPop 不等待结果

database - AWS Elasticache Redis 故障转移

docker - Alpine OS 上的 Php7 Redis 客户端

Python quicksort - 列表理解与递归(分区例程)

python | docker | Django | django.core.exceptions.ImproperlyConfigured 错误

python 社交身份验证未注册域