Warm tip: This article is reproduced from serverfault.com, please click

python-忍者 Flask 2

(python - Flask, Jinja2)

发布于 2020-12-02 01:15:29

我已经在这个错误上停留了很长时间了。我是Jinja2和Flask的新手,但是我尝试过弄乱我的for循环并编辑我的内容main.py无济于事。这是到目前为止我得到的。

首先,我的 main.py

# Data Science Notes Route
@app.route('/data-science/<content>')
def note(content):   
    return render_template('notes.html', content=content)

接下来,导致错误的模板部分。

{% for link in category_files %}

    <h2 class="file-title"><a href="{{ url_for('note', content=get_note_content(link)) }}"
            class="file-link">{{ note_dict[link].title }}</a></h2>

    <!-- Display a description of the file -->
    <p class="file-description">{{ note_dict[link].description }}</p>

    <!-- Display the topics covered -->
    <p class="file-topics">Topics covered: {{ note_dict[link].topics }}</p>

    <!-- Display an image -->
    <img src="{{ note_dict[link].image }}" alt="">

    {% endfor %}```

The issue is that very first part with the `a` tag.  According to the error message, I am missing the **content** argument.  But it looks to me like I'm not!  Here's the error output, as well as the `dump()` output. 

[![Image of Error][1]][1]

Any aid would be greatly appreciated!  Thanks!


  [1]: https://i.stack.imgur.com/iyqN0.png
Questioner
Luke Anglin
Viewed
11
PGHE 2020-12-02 09:37:41

你尚未将其包含filepath在URL路由中。

@app.route('/<note_title>/<filepath>', methods=['GET'])
def note(note_title, filepath):
    ....

如果要将文件路径作为url参数,则:

from flask import request
@app.route('/<note_title>', methods=['GET'])
def note(note_title):
    filepath = request.args.get('filepath')
    ....