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

Docker化Django Admin新增页面报错求助:AttributeError问题

Django项目迁移Docker后Admin新增页面内部服务器错误排查

问题描述

将虚拟机中正常运行的Django项目迁移至Docker容器,未修改任何Admin模板,但访问/admin/mainapp/divesite/add/时触发内部服务器错误,完整报错日志如下:

报错日志

dcdc_django    | Internal Server Error: /admin/mainapp/divesite/add/
dcdc_django    | Traceback (most recent call last):
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/core/handlers/exception.py", line 55, in inner
dcdc_django    |     response = get_response(request)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/core/handlers/base.py", line 220, in _get_response
dcdc_django    |     response = response.render()
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/response.py", line 114, in render
dcdc_django    |     self.content = self.rendered_content
dcdc_django    |                    ^^^^^^^^^^^^^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/response.py", line 92, in rendered_content
dcdc_django    |     return template.render(context, self._request)
dcdc_django    |            ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/backends/django.py", line 107, in render
dcdc_django    |     return self.template.render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 171, in render
dcdc_django    |     return self._render(context)
dcdc_django    |            ~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/test/utils.py", line 114, in instrumented_test_render
dcdc_django    |     return self.nodelist.render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render
dcdc_django    |     return SafeString("".join([node.render_annotated(context) for node in self]))
dcdc_django    |                                ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated
dcdc_django    |     return self.render(context)
dcdc_django    |            ~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/loader_tags.py", line 159, in render
dcdc_django    |     return compiled_parent._render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/test/utils.py", line 114, in instrumented_test_render
dcdc_django    |     return self.nodelist.render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render
dcdc_django    |     return SafeString("".join([node.render_annotated(context) for node in self]))
dcdc_django    |                                ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated
dcdc_django    |     return self.render(context)
dcdc_django    |            ~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/loader_tags.py", line 159, in render
dcdc_django    |     return compiled_parent._render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/test/utils.py", line 114, in instrumented_test_render
dcdc_django    |     return self.nodelist.render(context)
dcdc_django    |            ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render
dcdc_django    |     return SafeString("".join([node.render_annotated(context) for node in self]))
dcdc_django    |                                ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated
dcdc_django    |     return self.render(context)
dcdc_django    |            ~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/loader_tags.py", line 65, in render
dcdc_django    |     result = block.nodelist.render(context)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render
dcdc_django    |     return SafeString("".join([node.render_annotated(context) for node in self]))
dcdc_django    |                                ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated
dcdc_django    |     return self.render(context)
dcdc_django    |            ~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/loader_tags.py", line 65, in render
dcdc_django    |     result = block.nodelist.render(context)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 1008, in render
dcdc_django    |     return SafeString("".join([node.render_annotated(context) for node in self]))
dcdc_django    |                                ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/base.py", line 969, in render_annotated
dcdc_django    |     return self.render(context)
dcdc_django    |            ~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/contrib/admin/templatetags/base.py", line 45, in render
dcdc_django    |     return super().render(context)
dcdc_django    |            ~~~~~~~~~~~~~~^^^^^^^^^
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/library.py", line 273, in render
dcdc_django    |     new_context = context.new(_dict)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/context.py", line 275, in new
dcdc_django    |     new_context = super().new(values)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/context.py", line 112, in new
dcdc_django    |     new_context = copy(self)
dcdc_django    |   File "/usr/local/lib/python3.14/copy.py", line 82, in copy
dcdc_django    |     return copier(x)
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/context.py", line 160, in __copy__
dcdc_django    |     duplicate = super().__copy__()
dcdc_django    |   File "/usr/local/lib/python3.14/site-packages/django/template/context.py", line 41, in __copy__
dcdc_django    |     duplicate.dicts = self.dicts[:]
dcdc_django    |     ^^^^^^^^^^^^^^^
dcdc_django    | AttributeError: 'super' object has no attribute 'dicts' and no __dict__ for setting new attributes

问题原因

该错误由Python 3.14与当前Django版本不兼容导致:Python 3.14对super()返回的代理对象做了限制,不允许直接设置属性;而旧版Django的模板上下文拷贝逻辑中,尝试给super().__copy__()返回的对象设置dicts属性,触发了AttributeError。

解决方案

1. 降级Python版本(推荐)

修改Dockerfile中的基础镜像,使用Python 3.13及以下版本,示例:

FROM python:3.13-slim

Python 3.13及更早版本无此super()代理对象限制,可直接兼容旧版Django。

2. 升级Django版本

升级到支持Python 3.14的Django版本(如Django 5.1及以上,以官方兼容说明为准)。修改requirements.txt:

Django>=5.1

重新构建Docker镜像并安装新版依赖。

3. 临时hack修复(不推荐生产环境)

若暂时无法升级/降级,可修改Django的template/context.py文件:
找到Context类的__copy__方法,将:

def __copy__(self):
    duplicate = super().__copy__()
    duplicate.dicts = self.dicts[:]
    return duplicate

修改为:

def __copy__(self):
    duplicate = Context()
    duplicate.dicts = self.dicts[:]
    return duplicate

该方案绕过super().__copy__()直接创建Context实例,但属于临时修复,后续升级Django会被覆盖,不建议生产环境使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:30:56