python - /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'我的猜测是我写的模板不能完成这项工作?
解决方案
将您的路径更改为这样的东西,这样就可以了
path("wiki/<str:title_name>/", views.title, name="title")
推荐阅读
- javascript - 刷新页面后变为空白(AngularJS 状态)
- android - “IllegalStateException:systemuid 中的相同 jobid。” 安排新工作时
- r - R 数据表。在具有分组依据和条件的移动窗口上应用功能
- mysql - Mysql Group By 使用 Count 获取最新记录
- android - 来自另一个文件的 DrawerNavigator 不起作用
- ios - 关闭视图控制器时隐藏插座
- java - WebResourceResponse 无法从 HttpConnection (Android) 读取完整的输入流
- python - pyqt5应用程序中对话框和主窗口的国际化(翻译)
- angular - Angular 6/7 默认使用急切/延迟加载?
- python - 更新 Django POST 请求