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

Django访问媒体/静态资源触发serve()意外参数ducoment_root报错

Django访问/media资源触发500错误问题解答

报错原文

Internal Server Error: /media/productimg/1.png
Traceback (most recent call last):
File "C:\Users\sajja\Envs\fyp\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\sajja\Envs\fyp\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: serve() got an unexpected keyword argument 'ducoment_root'
[01/Sep/2021 03:39:53] "GET /static/app/webfonts/fa-solid-900.woff2 HTTP/1.1" 404 1852
[01/Sep/2021 03:39:53] "GET /media/productimg/1.png HTTP/1.1" 500 60242

错误原因

核心错误为路由配置时的拼写错误:你在配置media资源访问路由时,给django.views.static.serve视图传递的参数名写错,将正确的document_root拼错为ducoment_root(把document中o和u的顺序写反),serve视图没有对应名称的参数,因此抛出类型错误。
另外报错中同时存在静态字体文件404问题,是静态资源路由没有正确配置导致的。

排查解决步骤

  • 打开项目根目录的urls.py文件,定位media资源访问路由的配置代码
  • 将配置中错误的参数名ducoment_root修改为正确的document_root
  • 推荐直接使用Django封装好的静态资源路由配置写法,避免手动拼写错误,正确配置示例如下:
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # 此处保留你原有项目的其他路由配置
]

# 开发环境下追加静态资源路由
if settings.DEBUG:
    # 配置media资源路由解决500错误
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    # 配置static资源路由解决字体文件404错误
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  • 修改完成后重启Django开发服务,重新访问对应资源验证问题是否解决

内容的提问来源于stack exchange,提问作者SAJJAD ALI

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 00:24:00