如何在烧瓶中按字母顺序对帖子进行排序?

时间:2022-05-10 07:20:51

I have been following the tutorial provided by Flask. I'm trying to change things around a bit and make it fit the criterion for a glossary.

我一直在关注Flask提供的教程。我试图改变一些事情并使其符合词汇表的标准。

I suspect that my issue lies in this line of code in my flaskr.py file:

我怀疑我的问题在于我的flaskr.py文件中的这行代码:

cur = db.execute('select title, text from entries order by id desc')

The reason why I suspect this is because when I mess with it it breaks everything. As well, when I tried to "sort" everything it did nothing, oh and it says to order by id descending... that's mainly why.

我之所以怀疑这是因为当我弄乱它时会破坏一切。同样,当我试图“排序”它所做的一切时,哦,并且它说按顺序降序排序......这主要是为什么。

What I tried was:

我尝试的是:

@app.route('/order', methods=['POST'])
def order_entry():
    entries.sort()
    return entries

Which is probably crude and sort of silly, but I'm particularly new to programming. I can't find any other places in my code where entries are being ordered.

这可能是粗俗的,有点傻,但我对编程特别陌生。我在我的代码中找不到任何其他位置的条目。

I have looked for different ways to organize a dictionary alphabetically but haven't had too much luck making it work. As you can tell.

我已经找到了按字母顺序组织字典的不同方法,但没有太多的运气使其工作。你可以说。

1 个解决方案

#1


1  

Assuming this is the Flask tutorial you're following, I think your function is missing some things. Is entries some sort of global variable, or did you just remove the part where it was created? I've tried to combine your code with one of the examples from the tutorial, and added some comments.

假设这是你正在关注的Flask教程,我认为你的功能缺少一些东西。条目是某种全局变量,还是只删除了创建它的部分?我试图将您的代码与教程中的一个示例结合起来,并添加了一些注释。

@app.route('/order', methods=['POST'])
def order_entry():
    # the following line creates a 'cursor' which you need to retrieve data
    # from the database
    cur = g.db.execute('select title, text from entries order by id desc')

    # the following line uses that cursor ("cur"), fetches the data, 
    # turns it into a (unsorted) list of dictionaries 
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]

    # let's sort the list by the 'title' attribute now
    entries = sorted(entries, key=lambda d: d['title'])
    # or if you prefer, you could say: "entries.sort(key=lambda d:d['title']"

    # return the template with the sorted entries in
    return render_template('show_entries.html', entries=entries)

Now, I don't know know Flask at all, but I think this is the gist of what you want to do.

现在,我根本不认识Flask,但我认为这是你想要做的事情的要点。

You may want to go through some Python tutorials (before tackling Flask), since there are a few basic concepts that, once you grasp, I think will make everything else much easier.

您可能想要阅读一些Python教程(在处理Flask之前),因为有一些基本概念,一旦您掌握,我认为将使其他一切变得更容易。

#1


1  

Assuming this is the Flask tutorial you're following, I think your function is missing some things. Is entries some sort of global variable, or did you just remove the part where it was created? I've tried to combine your code with one of the examples from the tutorial, and added some comments.

假设这是你正在关注的Flask教程,我认为你的功能缺少一些东西。条目是某种全局变量,还是只删除了创建它的部分?我试图将您的代码与教程中的一个示例结合起来,并添加了一些注释。

@app.route('/order', methods=['POST'])
def order_entry():
    # the following line creates a 'cursor' which you need to retrieve data
    # from the database
    cur = g.db.execute('select title, text from entries order by id desc')

    # the following line uses that cursor ("cur"), fetches the data, 
    # turns it into a (unsorted) list of dictionaries 
    entries = [dict(title=row[0], text=row[1]) for row in cur.fetchall()]

    # let's sort the list by the 'title' attribute now
    entries = sorted(entries, key=lambda d: d['title'])
    # or if you prefer, you could say: "entries.sort(key=lambda d:d['title']"

    # return the template with the sorted entries in
    return render_template('show_entries.html', entries=entries)

Now, I don't know know Flask at all, but I think this is the gist of what you want to do.

现在,我根本不认识Flask,但我认为这是你想要做的事情的要点。

You may want to go through some Python tutorials (before tackling Flask), since there are a few basic concepts that, once you grasp, I think will make everything else much easier.

您可能想要阅读一些Python教程(在处理Flask之前),因为有一些基本概念,一旦您掌握,我认为将使其他一切变得更容易。