我已经在这个错误上停留了很长时间了。我是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
你尚未将其包含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')
....
这是一个很好的答案,所以我会接受,但是我认为我不能很好地解释我的问题。我会接受这一点,然后尝试重新发布,并提供更好的解释。谢谢你的帮助!
仅当它确实回答了您的问题时,才接受它。但是为了进一步澄清,您正在提供
filepath
,但是当您将参数放在函数括号内时,它们也需要出现在您的URL中。在您的问题中,该参数被url_for
视为filepath
请求参数,而不是URL的一部分。