【发布时间】:2021-06-08 09:18:18
【问题描述】:
我的 django 应用程序文件夹的“条目”文件夹中有一些 md 文件,我想获取它们,将它们转换为 HTML,然后渲染它们。 这是我的 util.py
def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
def convert_md(filename):
"""
Converts given md file to html
"""
#file= f"{filename}.md"
file= default_storage.open(f"{filename}.md")
md= file.read()
html= md.markdown(md)
return html
这是我的观点.py
def wiki(request, topic):
if util.get_entry(topic) != None:
html= util.convert_md(topic)
return render(request, "encyclopedia/pages.html", {
"title": topic, "body": html
})
else:
return render(request, "encyclopedia/pages.html", {
"title": "ERROR", "body": "THIS PAGE IS NOT AVALIABLE."
})
我也有……
path("wiki/<str:topic>", views.wiki)
在我的 urls.py 中,但我仍然收到 FileNotFoundError at /wiki/Python 错误 注意:我已经pip安装了markdown
【问题讨论】:
标签: python html django markdown