You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Django的维基风格项目点击条目触发TypeError: title参数传值重复

Django视图报错:TypeError: entry() got multiple values for argument 'title'

我用Django开发一个维基百科风格的课程项目,点击条目时触发如下错误:

TypeError: entry() got multiple values for argument 'title'

我的views.py中对应条目的视图代码如下:

@csrf_exempt
def entry(request, title):
    flag = util.get_entry(title)
    if flag is None:
        form = SearchForm()
        content = "The page you look for does not exist :("
        return render(request, "encyclopedia/error.html", {"form": form, "content": content})
    else:
        form = SearchForm()
        mdcontent = util.get_entry(title)
        htmlcontent = markdowner.convert(mdcontent)
        return render(request, "encyclopedia/entry.html", {
            "title": title, "content": htmlcontent, "form": form
        })

回溯信息:

Request Method: GET
Request URL: http://localhost/wiki/CSS

Traceback (most recent call last):
File "/workspaces/79201800/wiki/env/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request)
File "/workspaces/79201800/wiki/env/lib/python3.10/site-packages/django/core/handlers/base.py", line 198, in _get_response response = wrapped_callback(request, title, *callback_args, **callback_kwargs)
File "/workspaces/79201800/wiki/env/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs)

Exception Type: TypeError at /wiki/CSS
Exception Value: entry() got multiple values for argument 'title'

问题原因与解决办法

这个错误是因为视图函数entry被重复传递了title参数,大概率是URL路由配置错误导致的:

  • 检查项目的urls.py文件,找到对应/wiki/<title>/的路由规则,是否同时通过路径捕获了title参数,又额外传递了关键字参数title。比如错误写法:
path('wiki/<str:title>/', views.entry, {'title': 'default_value'})

这种写法会让Django把URL中捕获的CSS作为位置参数传给title,同时又把'default_value'作为关键字参数传给title,导致参数重复赋值。

  • 正确的路由写法应该只通过路径捕获参数,不要额外添加关键字参数:
path('wiki/<str:title>/', views.entry)

另外确认视图函数的参数顺序无误(你的代码中request作为第一个参数,title作为第二个,是符合Django规范的),修正路由后即可解决该错误。

内容的提问来源于stack exchange,提问作者Rumeysa Gelgi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 04:39:17