Else语句不起作用。获取错误:TypeError:markdown()缺少1个必需的位置参数:“text”

2024-10-01 00:35:03 发布

您现在位置:Python中文网/ 问答频道 /正文

Django代码用于创建一个条目页面,其中访问/wiki/title,其中title是百科全书条目的标题,应该呈现一个显示该百科全书条目内容的页面。视图应该通过调用适当的util函数来获取百科全书条目的内容。如果请求的条目不存在,则应向用户显示一个错误页面,表明未找到其请求的页面

但若语句工作正常,那个么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                                         
    })

url.py

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:entry>", views.entry, name="entry")
]

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

Tags: frompyimportreturntitlerequest错误util