python - 其他语句不起作用。出现错误:TypeError:markdown() 缺少 1 个必需的位置参数:'text'
问题描述
Django 代码将创建一个条目页面,其中 Visiting /wiki/title(其中TITLE是百科全书条目的标题)应该呈现一个显示该百科全书条目内容的页面。视图应该通过调用适当的 util 函数来获取百科全书条目的内容。如果请求的条目不存在,则应向用户显示一个错误页面,指示未找到他们请求的页面。
但是 if 语句工作正常,但 else 语句不起作用。当我浏览http://127.0.0.1:8000/wiki/CSS时,它应该将我导航到该专用页面。但我收到一个错误:markdown() 缺少 1 个必需的位置参数:'text'。请告知如何减轻此错误?
视图.py
from markdown import markdown
from django.shortcuts import render
from . import util
#EntryPage using title
def entry(request, entry):
#markdowner = markdown()
entryPage = util.get_entry(entry)
#Displays the requested entry page, if it exists
if entryPage is None:
return render(request, "encyclopedia/nonExistingEntry.html", {
"entryTitle": entry
})
# Title exists, convert md to HTML and return rendered template
else:
return render(request, "encyclopedia/entry.html", {
"entry": markdown().convert(entryPage),
"entryTitle": entry
})
网址.py
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry")
]
实用程序.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
解决方案
正如 Kaleba 所说,尝试将 views.py 中的行更改为
"entry": markdown(entryPage)