首页 > 解决方案 > /wiki/HTML 处的类型错误

问题描述

当我尝试加载我的 title.html 模板时,会发生此 TypeError。我的逻辑很简单:如果请求的条目不存在,则应向用户显示一个错误页面,指示未找到他们请求的页面。如果条目确实存在,则应向用户呈现一个显示条目内容的页面。页面标题应包含条目名称。

我的 urlpatterns(在 encyclopedia/urls.py 中):

urlpatterns = [
path("", views.index, name="index"),
path("wiki/", views.title, name="title"),]

我的观点(在 encyclopedia/views.py 内):

def title(request, title_name):
return render(request, "encyclopedia/title.html",{
    "entry": util.get_entry(title_name),
    "title": util.get_page_name(title_name)
})

当然还有 util.py 如下:

def get_entry(title):

try:
    f = default_storage.open(f"entries/{title}.md")
    return f.read().decode("utf-8")
except FileNotFoundError:
    return None
def get_page_name(title):
try:
    name = os.path.basename(f'entries/{title}')
    return name
except TypeError:
    return None

有关 title.html 的更多信息:

{% extends "encyclopedia/layout.html" %}

{% block title %}

<p>{{ title }}</p>

{% endblock title %}

{% block body %}
<p> {{ entry }}</p>
{% endblock body %}

由于我在 /wiki/HTML title() 得到了一个TypeError 有一个意外的关键字参数 'title'我的猜测是我写的模板不能完成这项工作?

标签: pythondjangotemplates

解决方案


将您的路径更改为这样的东西,这样就可以了

path("wiki/<str:title_name>/", views.title, name="title")

推荐阅读